生活随笔
收集整理的這篇文章主要介紹了
php多进程实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
來源:http://blog.csdn.net/e421083458/article/details/22186475
PHP多進程實現(xiàn)
PHP有一組進程控制函數(shù)(編譯時需要–enable-pcntl與posix擴展),使得php能在nginx系統(tǒng)中實現(xiàn)跟c一樣的創(chuàng)建子進程、使用exec函數(shù)執(zhí)行程序、處理信號等功能。
CentOS 6 下yum安裝php的,默認是不安裝pcntl的,因此需要單獨編譯安裝,首先下載對應(yīng)版本的php,解壓后
[plain] view plaincopyprint?
cd?php-version/ext/pcntl??phpize??./configure?&&?make?&&?make?install??cp?/usr/lib/php/modules/pcntl.so?/usr/lib64/php/modules/pcntl.so??echo?"extension=pcntl.so"?>>?/etc/php.ini??/etc/init.d/httpd?restart??
方便極了。
下面是示例代碼:
[php] view plaincopyprint?
<?php??header('content-type:text/html;charset=utf-8'?);??????if?(!function_exists("pcntl_fork"))?{??????die("pcntl?extention?is?must?!");??}????$totals?=?3;????$cmdArr?=?array();????for?($i?=?0;?$i?<?$totals;?$i++)?{??????$cmdArr[]?=?array("path"?=>?__DIR__?.?"/run.php",??'pid'?=>$i?,'total'?=>$totals);??}??????????????????????????????????pcntl_signal(SIGCHLD,?SIG_IGN);???foreach?($cmdArr??as???$cmd)?{??????$pid?=?pcntl_fork();????????????????if?($pid?==?-1)?{????????????????????die('could?not?fork');??????}?else?if?($pid)?{??????????????????????????????pcntl_wait($status,WNOHANG);???????}?else?{????????????????????$path???=?$cmd["path"];??????????$pid?=?$cmd['pid']?;??????????$total?=?$cmd['total']?;??????????echo?exec("/usr/bin/php?{$path}?{$pid}?{$total}")."\n";??????????exit(0)?;??????}??}???>?
總結(jié)
以上是生活随笔為你收集整理的php多进程实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。