【发布时间】:2019-09-16 22:27:24
【问题描述】:
所以我尝试使用 guzzle 处理几个并发请求。我在网上看过几个例子,这就是我想出的,但似乎无法让它发挥作用。没有错误,没有警告,什么都没有。我已经尝试在每个 Promise 中登录,但没有任何反应。
而且我确信没有任何事情发生,因为数据库中没有插入任何内容。有什么我想念的想法吗? (我使用其各自的then 生成每个请求,因为在每个承诺结束时,数据库操作特定于该用户)
use GuzzleHttp\Promise\EachPromise;
use Psr\Http\Message\ResponseInterface;
$promises = (function () use($userUrls){
$userUrls->each(function($user) {
yield $this->client->requestAsync('GET', $user->pivot->url)
->then(function (ResponseInterface $response) use ($user) {
$this->dom->load((string)$response->getBody());
// ... some db stuff that inserts row in table for this
// $user with stuff from this request
});
});
});
$all = new EachPromise($promises, [
'concurrency' => 4,
'fulfilled' => function () {
},
]);
$all->promise()->wait();
【问题讨论】: