PHP之pcntl_fork多进程并发编程示例
待下载的网页地址放在$urls数组中,按指定的并发数多进程下载网页,下载的网页保存在本地硬盘,下载的网页大小通过linux消息队列发送给父进程累加,全部网页下载完成后,父进程显示下载的网页数、字节数。代码如下。
<?//$urls数组用于保存要下载的网址,实际应用中一般从文件或数据库中读取网址保存到$urls中。$urls=array('http://www.qq.com','http://www.sohu.com','http://www.sina.com.cn',....);$urls_num=count($urls);//数组大小,也是网址数量$msg_file="/tmp/download_msgqueue.txt";//下面3行创建linux消息队列下,该文件须先创建好$msg_queuekey=ftok($msg_file,'R');//touch/tmp/download_msgqueue.txt$msg_queue=msg_get_queue($msg_queuekey,0666);$maxtasknum=5;//设定并发进程数$ct=0;//$urls数组用的计数器$cttask=0;//并发进程计数器$pids=array();//保存进程的数组$total_bytes=0;//下载网页的字节数while($ct<$urls_num){//循环抓取$urls数组中指定的网页while($cttask<$maxtasknum&&$ctproc<$urls_num){//fork出指定的并发数进程$pids[$ct]=pcntl_fork();if($pids[$ct]==-1){echo"createsubprocfail.\n";exit(0);}elseif($pids[$ct]>0){//父进程}elseif($pids[$ct]==0){//子进程download($urls[$ct],$msg_queue);exit(0);}$cttask++;$ct++;}$tmppid=pcntl_waitpid(0,$status);//等待子进程结束foreach($pidsas$key=>$pid){if($tmppid==$pid){unset($pids[$key]);$cttask--;//子进程结束后,并发进程计数器减1}}do{//从消息队列出取出每个网页的大小,计算下载的字节数。如果要下载的网页很多,需要把此段代码放到下载网页的循环中,否则可能会出现队列满的情况。msg_receive($msg_queue,0,$message_type,16,$message,true,MSG_IPC_NOWAIT);//echo"[".$message."]\n";$total_bytes+=$message;$a=msg_stat_queue($msg_queue);if($a['msg_qnum']==0){//这种方式退出比$ct==$urls_num好,因为如果fork==-1,就不会有$urls_num个消息,程序会永远等待消息。break;}}while(true);}while($cttask>0){//等待最后$cttask个子进程结束$tmppid=pcntl_waitpid(0,$status);foreach($pidsas$key=>$pid){if($tmppid==$pid){unset($pids[$key]);$cttask--;}}}do{//取得最后$cttask个子进程的消息msg_receive($msg_queue,0,$message_type,16,$message,true,MSG_IPC_NOWAIT);//echo"[".$message."]\n";$total_bytes+=$message;$a=msg_stat_queue($msg_queue);if($a['msg_qnum']==0){break;}}while(true);msg_remove_queue($msg_queue);//删除消息队列echo"\nDone.download:".$urls_num."pages,total:".round($total_bytes/1024,3)."KB\n";exit(0);functiondownload($url,$msg_queue){//下载指定网页,把内容保存在本地硬盘,并下载内容的长度放入消息队列中$dirname="/tmp/donwload/";//保存下载网页的目录,要事先创建好$content=file_get_contents($url);if($content===false){$content=0;}$url_parts=parse_url($url);$fname=$dirname.$url_parts['host'];$ret=file_put_contents($fname,$content);msg_send($msg_queue,1,strlen($content));}?>
参考资料:
PHP实现进程间通信:消息队列 https://www.douban.com/note/245520545/
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。