我们正在尝试使用guzzle进行并发异步请求。在浏览了一些资源(例如this和this)后,我们想出了一些在下面分享的代码。但是它没有按预期工作。
看起来Guzzle正在同步执行这些请求而不是异步。
仅出于测试目的,我们正在打一个内部网址,它可以进行5秒的睡眠。在并发性为10的情况下,我们希望所有10个请求最初都会排队并同时发送到服务器几乎,然后等待5秒,然后几乎全部其中几乎将在同一时间完成。这将使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
操作系统/版本信息
问题可能是\ GuzzleHttp \ Promise \ each_limit ..这可能不会足够快地启动或解决承诺。我们可能必须将其转换为tick
外部。
答案 0 :(得分:10)
在示例代码中,您要为要生成的每个请求创建新的GuzzleHttp\Client
实例。这似乎并不重要,但是,在GuzzleHttp\Client
实例化期间,如果没有提供默认值handler,它将设置默认值this。 (然后将此值传递给通过客户端发送的任何请求,除非被覆盖。)
注意:它确定了curl_multi_exec
docs函数中使用的最佳处理程序。但是,它很可能最终会默认为curl_mutli_exec
。
这个的重要性是什么?它是负责同时跟踪和执行多个请求的底层处理程序。通过每次创建一个新的处理程序,您的所有请求都无法正确分组并一起运行。有关这方面的更多信息,请参阅{{3}}。
所以,你有两种方法可以解决这个问题:
通过客户端传递给迭代器:
$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();