PHP实现多进程并行操作的详解(可做守护进程)
如下所示:
代码如下:
<font style="COLOR: #ff0000"><strong>/** * 入口函数 * 将此文件保存为 ProcessOpera.php * 在terminal中运行 /usr/local/php/bin/php ProcessOpera.php & * 查看进程 ps aux|grep php */ </strong></font>ProcessOpera("runCode", array(), 8); /** * run Code */ function runCode($opt = array()) { //需要在守护进程中运行的代码 } /** * $func为子进程执行具体事物的函数名称 * $opt为$func的参数 数组形式 * $pNum 为fork的子进程数量 */ function ProcessOpera($func, $opts = array(), $pNum = 1) { while(true) { $pid = pcntl_fork(); if($pid == -1) { exit("pid fork error"); } if($pid) { static $execute = 0; $execute++; if($execute >= $pNum) { pcntl_wait($status); $execute--; } } else { while(true) { //somecode $func($opts); sleep(1); } exit(0); } } }
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23