guzzle并发请求,在发送下一个之前等待完成批处理

时间:2016-10-25 14:07:11

标签: php curl concurrency guzzle guzzle6

我认为以下代码可以这样工作:

  1. 发送CONCURRENT_REQUESTS的批次
  2. 等待所有这些请求完成
  3. 以上批次的下一批是发送
  4. 但实际上如果我评论第14行[usleep(...)],似乎请求批次尽可能快地发送给服务器生成数千个查询。 是否有可能改变它?如何更改此行为?

    <?php
    $pool = $this->getPool();
    if (false !== $pool) {
        $pool->promise()->wait();
    }
    private function getPool()
    {
      $requests = function ($data) {
        foreach ($data as $index => $datum) {
            yield $this->patch($datum)->then(function (
                    $response
                ) use ($index) {
                    usleep(SLEEP_TIME_IN_SECONDS *1000000);
                    return [
                        'response' => $response,
                        'index'    => $index
                    ];
                });
            }
        };
        return new EachPromise($requests($data), [
            'concurrency' => CONCURRENT_REQUESTS,
            'fulfilled'   => function ($response, $index) use ($data) {
                // log
            },
            'rejected'    => function ($reason, $index) use ($data) {
                // do stuff
            }
        ]);
    }
    private function patch($data)
    {
        $request = new Request(REQUEST_TYPE_PATCH, $url, $this->getPatchHeaders());
        return $this->client->sendAsync($request);
    }
    

1 个答案:

答案 0 :(得分:3)

它适用于我(使用相同的(通过含义)代码。

use GuzzleHttp\Client;
use function GuzzleHttp\Promise\each_limit;

$client = new Client();

$requests = function () use ($client) {
    foreach (range(1, 15) as $index) {
        echo "Starting $index query...\n";

        yield $client->getAsync('http://google.com/')
            ->then(function ($response) use ($index) {
                echo "Request $index completed successfully.\n";

                return [
                    'response' => $response,
                    'index'    => $index
                ];
            });
    }
};

$promise = each_limit(
    $requests(),
    3
    // fulfiled
    // rejected
);

$promise->wait();

结果是:

Starting 1 query...
Starting 2 query...
Starting 3 query...
Request 3 completed successfully.
Starting 4 query...
Request 2 completed successfully.
Starting 5 query...
Request 4 completed successfully.
Starting 6 query...
Request 1 completed successfully.
Starting 7 query...
Request 5 completed successfully.
Starting 8 query...
Request 6 completed successfully.
Starting 9 query...
Request 7 completed successfully.
Starting 10 query...
Request 8 completed successfully.
Starting 11 query...
Request 9 completed successfully.
Starting 12 query...
Request 10 completed successfully.
Starting 13 query...
Request 11 completed successfully.
Starting 14 query...
Request 12 completed successfully.
Starting 15 query...
Request 13 completed successfully.
Request 14 completed successfully.
Request 15 completed successfully.

所以它确实有效。请再次检查您的代码。尝试更新到最新版本的Guzzle(我尝试过6.2.2)。