Home >Backend Development >PHP Tutorial >Php multi-process implementation programming example
This article mainly introduces Php multi-process implementation programming examples, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
php multi-process implementation
PHP has a set of process control functions (-enable-pcntl and posix extensions are required when compiling), which enables PHP to create child processes, use the exec function to execute programs, and handle signals in the nginx system just like c. and other functions.
When installing php under yum under CentOS 6, pcntl is not installed by default, so you need to compile and install it separately. First download the corresponding version of php, and unzip it
cd php-version/ext/pcntl phpize ./configure && make && make install cp /usr/lib/php/modules/pcntl.so /usr/lib64/php/modules/pcntl.so echo "extension=pcntl.so" >> /etc/php.ini /etc/init.d/httpd restart
Convenient Extremely.
The following is the sample code:
<?php header('content-type:text/html;charset=utf-8' ); // 必须加载扩展 if (!function_exists("pcntl_fork")) { die("pcntl extention is must !"); } //总进程的数量 $totals = 3; // 执行的脚本数量 $cmdArr = array(); // 执行的脚本数量的数组 for ($i = 0; $i < $totals; $i++) { $cmdArr[] = array("path" => __DIR__ . "/run.php", 'pid' =>$i ,'total' =>$totals); } /* 展开:$cmdArr Array ( [0] => Array ( [path] => /var/www/html/company/pcntl/run.php [pid] => 0 [total] => 3 ) [1] => Array ( [path] => /var/www/html/company/pcntl/run.php [pid] => 1 [total] => 3 ) [2] => Array ( [path] => /var/www/html/company/pcntl/run.php [pid] => 2 [total] => 3 ) ) */ pcntl_signal(SIGCHLD, SIG_IGN); //如果父进程不关心子进程什么时候结束,子进程结束后,内核会回收。 foreach ($cmdArr as $cmd) { $pid = pcntl_fork(); //创建子进程 //父进程和子进程都会执行下面代码 if ($pid == -1) { //错误处理:创建子进程失败时返回-1. die('could not fork'); } else if ($pid) { //父进程会得到子进程号,所以这里是父进程执行的逻辑 //如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成: pcntl_wait($status,WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。 } else { //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。 $path = $cmd["path"]; $pid = $cmd['pid'] ; $total = $cmd['total'] ; echo exec("/usr/bin/php {$path} {$pid} {$total}")."\n"; exit(0) ; } } ?>##Related recommendations:
PHP and more Process processing of parallel tasks
Detailed explanation of PHP multi-process programming examples
The above is the detailed content of Php multi-process implementation programming example. For more information, please follow other related articles on the PHP Chinese website!