Guzzle HTTP异步池请求

时间:2017-02-20 09:00:18

标签: php http asynchronous guzzle guzzlehttp

我有以下代码:

$client = new Client($conf);

//Pool of requests
$requests = function ($data) use ($client) {
    yield function () use ($client, $pixel) {
        ....
    }
}
//Pool of fullfilled
$fullfilled = function ($response, $index) use ($data) {
    ...
}
//Pool of rejected
$rejected = function ($reason, $index) use ($data) {
    ...
}

$pool = new Pool($client, $requests($data), [
            'concurrency' => 10, 'fulfilled' => $fullfilled, 'rejected' => $rejected
        ]);

// Initiate the transfers and create a promise
$promise = $pool->promise(); 
$promise->wait();

我不是因为我正在使用“等待”,但我希望该过程是异步的。 当前请求挂起,如果我省略等待,则池将不会被发送。 想法?

1 个答案:

答案 0 :(得分:1)

Guzzle文档描述了如何使用promises创建多个并发请求:http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests