search
Homephp教程php手册Linux下实现PHP多进程的方法分享

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

Jun 13, 2016 am 11:59 AM
controllinuxphpprocessDownuseshareaccomplishmethodofprocess

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");
?>

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development 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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft