Home  >  Article  >  php教程  >  Linux下实现PHP多进程的函数如何写

Linux下实现PHP多进程的函数如何写

WBOY
WBOYOriginal
2016-06-06 20:41:161104browse

PHP多进程:使用PHP的Process Control Functions(PCNTL/线程控制函数),需要的朋友可以参考下

PHP多进程:使用PHP的Process Control Functions(PCNTL/线程控制函数)
函数参考可见:
只能用在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");
?>
Statement:
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