With regard to the multi-process of Perl, you may soon think of parallel::forkmanager this module. But today we try to write a similar frame ourselves:)
This multi-process development model LIGHTTPD from the Open Source server framework, the core idea is that the parent process monitors the state of the child process and is responsible for the recovery, and the child process is responsible for the execution of the task. The maximum number of concurrent child processes can be set at the current preset value. If you need to involve inter-process communication, you can create a pipeline pipe, so that the child process before the end of the data through the shared pipeline to write to the parent process to read at a timely manner.
I in the work process encountered such a scenario, such as from the DB to take a number of tasks, receive packets and so on (constant fetch/receive), in order to ensure efficiency, generally take concurrent execution; The script is primarily lightweight and easy to deploy. This type of Watcher-worker mode is ideal for this scenario. The Perl code is as follows:
1 #! / Usr / bin / perl -w
2
3 #author: pandaychen
4 #date: 2015-04-01
5 #info: Implementing a Simple Multi-Process Framework
6
7 use strict;
8 use warnings;
9 use POSIX ": sys_wait_h";
10
11 my $ max_process = 20;
12 my $ cur_process = 0;
13
14 my @pid_array;
15
16 print "perl fork watcher-worker: \ n";
17
18 #main start here
19 CreateWorker ($ max_process);
20
21 #child start here
22 do_workers ();
twenty three
24 sub CreateWorker {
25 my $ workers = shift (@_);
26 print $ workers, "\ n";
27
28 my $ sign = 1;
29
30 while ($ sign) {
31 if ($ workers> 0) {
32 my $ pid = fork ();
33 if ($ pid> 0) {
34 #watcher
35 $ sign = 1;
36-$ workers;
37}
38 elsif ($ pid == 0) {
39 #workers
40 $ sign = 0;
41 print "create worker", $$, "success! \ N";
42 do_workers ();
43}
44 else
45 {
46 print "fork error:% s \ n";
47 return -1;
48}
49}
50 else {
51 #full workers
52 my $ nStatus;
53 while ((my $ collect = waitpid (-1, WNOHANG))> 0)
54 {
55 print $ collect, "child process \ n";
56 $ workers ++;
57}
58}
59}
60 return 0;
61}
62
63 sub do_workers {
64 #Simulate child process work
65 my $ rand = int (rand (10));
66 sleep ($ rand);
67 print "child process sleep", $ rand, "seconds. \ N";
68 exit (1);
69}
The results of the implementation are as follows:
Perl's multi-process framework (Watcher-worker)