Guzzle 异步请求不是真正的异步?

Guzzle async requests not really async?

问题

我们正在尝试使用 guzzle 进行并发异步请求。在浏览了一些资源(例如 this and this)之后,我们得出了一些代码,在下面共享。但是它没有按预期工作。

看起来 Guzzle 正在同步而不是异步执行这些请求。

预期

仅出于测试目的,我们正在触发一个内部 url,它会休眠 5 秒。并发数为 10 时,我们预计所有 10 个请求最初都会排队并 almost 同时发送到服务器,它们将等待 5 秒,然后 almost 所有这些几乎同时完成。这将使 guzzle 客户端从迭代器等中接收 10 个新请求。

代码

    $iterator = function() {
        $index = 0;
        while (true) {
            $client = new Client(['timeout'=>20]);
            $url = 'http://localhost/wait/5' . $index++;
            $request = new Request('GET',$url, []);
            echo "Queuing $url @ " . (new Carbon())->format('Y-m-d H:i:s') . PHP_EOL;
            yield $client
                ->sendAsync($request)
                ->then(function(Response $response) use ($request) {
                    return [$request, $response];
                });
        }
    };

    $promise = \GuzzleHttp\Promise\each_limit(
        $iterator(),
        10,  /// concurrency,
        function($result, $index) {
            /** GuzzleHttp\Psr7\Request $request */
            list($request, $response) = $result;
            echo (string) $request->getUri() . ' completed '.PHP_EOL;
        },
        function(RequestException $reason, $index) {
            // left empty for brevity
        }
    );
    $promise->wait();

实际结果

我们发现 Guzzle 在第一个请求完成之前不会发出第二个请求。等等。

Queuing http://localhost/wait/5/1 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/2 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/3 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/4 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/5 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/6 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/7 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/8 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/9 @ 2017-09-01 17:15:28
Queuing http://localhost/wait/5/10 @ 2017-09-01 17:15:28
http://localhost/wait/5/1 completed
Queuing http://localhost/wait/5/11 @ 2017-09-01 17:15:34
http://localhost/wait/5/2 completed
Queuing http://localhost/wait/5/12 @ 2017-09-01 17:15:39
http://localhost/wait/5/3 completed
Queuing http://localhost/wait/5/13 @ 2017-09-01 17:15:45
http://localhost/wait/5/4 completed
Queuing http://localhost/wait/5/14 @ 2017-09-01 17:15:50 

OS/版本信息

问题可能出在 \GuzzleHttp\Promise\each_limit .. 它可能没有足够快地启动或解决承诺。我们可能必须将其欺骗到 tick 外部。

在示例代码中,您要为每个要发出的请求创建一个新的 GuzzleHttp\Client 实例。这可能看起来并不重要,但是,在 GuzzleHttp\Client 的实例化期间,如果提供了 none,它将设置默认值 handler。 (此值然后传递给通过客户端发送的任何请求,除非它被覆盖。)

注意:它决定了使用 this 函数的最佳处理程序。不过,它很可能最终会默认为 curl_mutli_exec.

这有什么重要性?它是负责同时跟踪和执行多个请求的底层处理程序。通过每次都创建一个新的处理程序,none 的请求被正确地分组并 运行 在一起。要对此有更深入的了解,请查看 curl_multi_exec docs.

所以,您有两种处理方法:

通过客户端传递给迭代器:

$client = new GuzzleHttp\Client(['timeout' => 20]);

$iterator = function () use ($client) {
    $index = 0;
    while (true) {
        if ($index === 10) {
            break;
        }

        $url = 'http://localhost/wait/5/' . $index++;
        $request = new Request('GET', $url, []);

        echo "Queuing $url @ " . (new Carbon())->format('Y-m-d H:i:s') . PHP_EOL;

        yield $client
            ->sendAsync($request)
            ->then(function (Response $response) use ($request) {
                return [$request, $response];
            });

    }
};

$promise = \GuzzleHttp\Promise\each_limit(
    $iterator(),
    10,  /// concurrency,
    function ($result, $index) {
        /** @var GuzzleHttp\Psr7\Request $request */
        list($request, $response) = $result;
        echo (string)$request->getUri() . ' completed ' . PHP_EOL;
    }
);
$promise->wait();

或在别处创建处理程序并将其传递给客户端:(虽然我不确定您为什么要这样做,但它就在那里!)

$handler = \GuzzleHttp\HandlerStack::create();

$iterator = function () use ($handler) {
    $index = 0;
    while (true) {
        if ($index === 10) {
            break;
        }

        $client = new Client(['timeout' => 20, 'handler' => $handler])
        $url = 'http://localhost/wait/5/' . $index++;
        $request = new Request('GET', $url, []);

        echo "Queuing $url @ " . (new Carbon())->format('Y-m-d H:i:s') . PHP_EOL;

        yield $client
            ->sendAsync($request)
            ->then(function (Response $response) use ($request) {
                return [$request, $response];
            });

    }
};

$promise = \GuzzleHttp\Promise\each_limit(
    $iterator(),
    10,  /// concurrency,
    function ($result, $index) {
        /** @var GuzzleHttp\Psr7\Request $request */
        list($request, $response) = $result;
        echo (string)$request->getUri() . ' completed ' . PHP_EOL;
    }
);
$promise->wait();