1

是否可以在不使用pthread或之类的任何扩展的情况下并行运行两种方法pcntl

我尝试了Symfony/Process,但似乎我只能将它用于 php cli 编程。

我的要求是php web application。我发现amphp/parallel说它无需任何扩展即可工作。我尝试了以下示例。虽然我不知道该库是否提供真正的并行功能或其他类似排队任务的功能?因为它在下面评论“没有任何扩展并行处理是不可能的”。

在我尝试之前,amphp/parallel我想确定我找到的图书馆是否适合我的工作。假设我想并行运行两个方法 test1() 和 test2() 并在下一行使用这两个方法的结果。

    <?php

    require __DIR__ . '/../vendor/autoload.php';

    use Amp\Parallel\Worker;
    use Amp\Promise;

    $urls = [
        'https://secure.php.net',
        'https://amphp.org',
        'https://github.com',
    ];

    $promises = [];
    foreach ($urls as $url) {
        $promises[$url] = Worker\enqueueCallable('file_get_contents', $url);
    }

    $responses = Promise\wait(Promise\all($promises));

    foreach ($responses as $url => $response) {
        \printf("Read %d bytes from %s\n", \strlen($response), $url);
test1(){
   // do some database query
}

test2(){
  // run a lengthy for loop
}
4

0 回答 0