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条)

报告相同问题?