【问题标题】:Guzzle concurrent requests - which is the requested url from the responseGuzzle 并发请求 - 这是响应中请求的 url
【发布时间】:2018-07-22 21:19:07
【问题描述】:

我使用 guzzle 库发出了一些并发请求。完成此操作的代码:

public function concurrentRequests(array $uri)
{
    $client = $this;
    $requests = function ($total) use ($client, $uri) {
        for ($i = 0; $i < $total; $i++) {
            yield function () use ($client, $uri, $i) {
                return $client->requestAsync('GET', $uri[$i]);
            };
        }
    };

    $pool = Pool::batch($client, $requests(count($uri)), [
        'concurrency' => 5,
        'fulfilled' => function ($response, $index) {
            // this is delivered each successful response
        },
        'rejected' => function ($reason, $index) {
            // this is delivered each failed request
        },
    ]);

    return $pool;
}

我想知道每个响应属于哪个请求。在迭代响应时有什么办法吗?

【问题讨论】:

    标签: php concurrency guzzle


    【解决方案1】:

    这很简单,因为方法 Pool::batch

    以与发送请求相同的顺序返回包含响应或异常的数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-22
      • 2018-08-04
      • 2020-03-22
      • 1970-01-01
      • 2017-09-17
      • 2019-09-08
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多