Home > Backend Development > PHP Tutorial > PHP pcntl multi-process usage example_PHP tutorial

PHP pcntl multi-process usage example_PHP tutorial

WBOY
Release: 2016-07-13 10:02:16
Original
687 people have browsed it

Examples of pcntl multi-process usage in PHP

This article mainly introduces the usage of pcntl multi-process in PHP. The example analyzes the usage skills of pcntl operating multi-process, which is of great practical value. , friends in need can refer to it

The example in this article describes the usage of PHP's pcntl multi-process. Share it with everyone for your reference. The specific analysis is as follows:

PHP can also process a transaction using multiple processes using the PCNTL series of functions. For example, I need to obtain 800,000 pieces of data from the database and then do a series of subsequent processing. At this time, should I use a single process? You can wait until today next year. So you should use the pcntl function.

Suppose I want to start 20 processes and divide 1-80w of data into 20 parts. The main process waits for all child processes to finish before exiting:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

$max = 800000;

$workers = 20;

$pids = array();

for($i = 0; $i < $workers; $i ){

$pids[$i] = pcntl_fork();

switch ($pids[$i]) {

case -1:

echo "fork error : {$i} rn";

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);

}

}

1 2

3

4

5

6 7

89 10 11 12 13 14
15
16
17 18 19 20 21 22 23 24 25
$max = 800000; $workers = 20; $pids = array(); for($i = 0; $i < $workers; $i ){<🎜> <🎜>$pids[$i] = pcntl_fork();<🎜> <🎜>switch ($pids[$i]) {<🎜> <🎜>case -1:<🎜> <🎜>echo "fork error : {$i} rn";<🎜> <🎜>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); } }
When pcntl_fork comes out, a pid value will be returned. This pid is 0 in the child process, and is the pid of the child process in the parent process (>0). If the pid is -1, it means that the fork went wrong. . Use a $pids array to let the main process wait for all processes to complete before ending I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/970755.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970755.htmlTechArticlePHP’s pcntl multi-process usage example This article mainly introduces PHP’s pcntl multi-process usage, and the example analyzes pcntl The tips for operating multiple processes are of great practical value. Friends who need them can...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template