search
Homephp教程php手册Linux下实现PHP多进程的函数如何写

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools