php pcntl 并发编程,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

while ($cttask

$pids[$ct] = pcntl_fork();

if ($pids[$ct]==-1) {

echo "create subproc fail.\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($pids as $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($pids as $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);

function download($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/

原文:http://happy366.blog.51cto.com/2203682/1769993

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值