Home > Backend Development > PHP Tutorial > Detailed explanation of multi-process parallel operation in PHP (can be used as daemon process)_PHP tutorial

Detailed explanation of multi-process parallel operation in PHP (can be used as daemon process)_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:06:10
Original
1029 people have browsed it

As shown below:

Copy the code The code is as follows:

/**
* Entry function
* Save this file as ProcessOpera.php
* Run /usr/local/php/bin/php ProcessOpera.php &
in terminal php
*/
ProcessOpera("runCode", array(), 8);

/**
 * run Code
 */
function runCode($opt = array()) {
//Code that needs to be run in the daemon process
}

/**
* $func is the function name of the child process to perform specific things
* $opt is the parameter of $func in array form
* $pNum is the number of child processes for fork
*/
function ProcessOpera($func, $opts = array(), $pNum = 1) {
while(true) {
$pid = pcntl_fork();
if($pid == -1) {
exit("pid fork error");
}
if($pid) {
static $execute = 0;
$execute++;
if($execute >= $pNum) {
pcntl_wait($status);
$execute--;
}
} else {
while(true) {
//somecode
$func($opts);
sleep(1);
}
exit(0);
}
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327645.htmlTechArticle is as follows: Copy the code as follows: /** * Entry function* Save this file as ProcessOpera.php * Run /usr/local/php/bin/php ProcessOpera.php nbsp;* View the process...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template