douguan1887 2017-04-06 15:23
浏览 85
已采纳

与popen平行的过程

I need to download files from many web servers and then do somethigs. Some files are big. Each downloads it's independent from another so I want to do in parallel.

So I write the following scripts:

test.php

$urls = [
    'url1', 'url2', 'url3'
];

foreach($urls as $url){
    popen("php download.php --url=" . $url, "w");
}

download.php

$options = getopt(null, ["url:"]);
$url = $options["url"];
// do somethings - This loop is just for testing purposes
for($i = 0; $i <= 1000000000; $i++){
    $i;
}
print parse_url($url)["host"];

When i run php test.php console show me a sequencial output. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doupeng8419 2017-04-08 00:41
    关注

    Finally, I found the best solution.

    Pool was covered all my requirements. Pool-worker is a software design pattern in such Pool is a container and has many Workers (Process waiting for tasks). tasks are added to the pool and then, each worker take a task to exec.

    By the other hand, I think that Python's implementation is better that PHP's implementation of Pool-worker pattern than PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害