Php multi-process implementation programming example

不言
Release: 2023-03-25 14:06:02
Original
1614 people have browsed it

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
Copy after login

Convenient Extremely.

The following is the sample code:

<?php 
header(&#39;content-type:text/html;charset=utf-8&#39; ); 
 
// 必须加载扩展 
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", &#39;pid&#39; =>$i ,&#39;total&#39; =>$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(&#39;could not fork&#39;); 
 } else if ($pid) { 
  //父进程会得到子进程号,所以这里是父进程执行的逻辑 
  //如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成: 
  pcntl_wait($status,WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。 
 } else { 
  //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。 
  $path = $cmd["path"]; 
  $pid = $cmd[&#39;pid&#39;] ; 
  $total = $cmd[&#39;total&#39;] ; 
  echo exec("/usr/bin/php {$path} {$pid} {$total}")."\n"; 
  exit(0) ; 
 } 
} 
?>
Copy after login

##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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!