Linux下实现PHP多进程的方法分享_php技巧

WBOY
Release: 2016-05-17 09:09:50
Original
879 people have browsed it

PHP多进程:使用PHP的Process Control Functions(PCNTL/线程控制函数)
函数参考可见:http://www.php.net/manual/zh/ref.pcntl.php
只能用在Unix Like OS,Windows不可用。
编译php的时候,需要加上–enable-pcntl,且推荐仅仅在CLI模式运行,不要在WEB服务器环境运行。

以下为实现PHP多进程的简单测试代码:

复制代码 代码如下:

declare(ticks=1);
$bWaitFlag = FALSE; /// 是否等待进程结束
$intNum = 10; /// 进程总数
$pids = array(); /// 进程PID数组

echo ("Start\n");

for($i = 0; $i
$pids[$i] = pcntl_fork();/// 产生子进程,而且从当前行之下开试运行代码,而且不继承父进程的数据信息

if(!$pids[$i]) {
// 子进程进程代码段_Start
$str="";
sleep(5+$i);
for ($j=0;$jecho "$i -> " . time() . " $str \n";
exit();
// 子进程进程代码段_End
}

}
if ($bWaitFlag)
{
for($i = 0; $i pcntl_waitpid($pids[$i], $status, WUNTRACED);
echo "wait $i -> " . time() . "\n";
}
}
echo ("End\n");
?>
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