PHP's PCNTL Multi-process Usage example
This article mainly introduced the PHP pcntl multi-process usage, the example analyzes the PCNTL operation multi-process use skill, very has the practical value, needs the friend to refer
The example in this paper describes the PCNTL multi-process usage of PHP. Share to everyone for your reference. The specific analysis is as follows:
PHP uses the PCNTL series of functions to do a multi-process processing of a transaction. For example, I need to get 80w data from the database, and then do a series of subsequent processing, this time, with a single process? You can wait until next year today. So you should use the PCNTL function.
Suppose I want to start 20 processes, divide the 1-80w data into 20 parts, and the main process waits for all the child processes to end before exiting:
?
1 2 3 4 5 6 7 8 9 Ten One + All + + 2 0 ( ) , , , |
$max = 800000; $workers =; $pids = array (); for ($i = 0; $i < $workers; $i + +) { $pids [$i] = Pcntl_fork (); Switch ($pids [$i]) { case-1: echo "Fork error: {$i} \ r \ n"; Exit; Case 0: $param = Array ( ' LastID ' + $max/$workers * $i, ' maxid ' = + $max/$workers * ($i + 1), ), $this->executeworker ($input, $output, $param); Exit; Default: Break; } } foreach ($pids as $i = + $pid) { if ($pid) { Pcntl_waitpid ($pid, $status); } } |
Here, when the pcntl_fork comes out, it returns a PID value, which is 0 in the child process, the PID (>0) of the subprocess in the parent process, and if the PID is 1, the fork is wrong.
Using an $pids array allows the main process to wait for the end of all processes.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/970755.html www.bkjia.com true http://www.bkjia.com/PHPjc/970755.html techarticle PHP pcntl Multi-Process Usage example this article mainly introduces the PHP pcntl multi-process usage, the example analyzes the PCNTL operation multi-process use skill, very has the practical value, needs the friend to be able ...