大量并发请求,等待完成的批处理再发送下一个 [英] guzzle concurrent request, wait for finished batch before sending next

查看:184
本文介绍了大量并发请求,等待完成的批处理再发送下一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为以下代码将以这种方式工作:

I thought this following code would work this way:

  1. 已发送数量为 CONCURRENT_REQUESTS 的批次
  2. 等待所有这些请求完成
  3. 已发送上述编号的下一批
  4. 依此类推

但是实际上,如果我在第14行注释[ usleep(...)],似乎请求批处理的发送速度尽可能快,从而向服务器生成了数千个查询.可以更改吗?如何更改此行为?

but in reality if I comment line 14 [usleep(...)] it seems the request batches are send as fast as possible generating thousands of queries to the server. Is it possible to change it? How do I change this behavior?

<?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);
}

推荐答案

它对我来说具有相同(含义)的代码.

It works for me with the same (by meaning) code.

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

So it really works. Please check your code again. Try to update to the latest version of Guzzle (I tried with 6.2.2).

这篇关于大量并发请求,等待完成的批处理再发送下一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆