我使用Guzzle(http://guzzlephp.org)獲取大量網址(約300k)。這些網址是從彈性搜索實例中檢索的,我想繼續向網址池添加網址,以便網池保持相當小的狀態,而不是一次添加全部網址。如何動態添加額外的請求到一個Guzzle池?
這可能嗎?我看着Pool.php,但沒有找到辦法做到這一點。有沒有辦法?
我使用Guzzle(http://guzzlephp.org)獲取大量網址(約300k)。這些網址是從彈性搜索實例中檢索的,我想繼續向網址池添加網址,以便網池保持相當小的狀態,而不是一次添加全部網址。如何動態添加額外的請求到一個Guzzle池?
這可能嗎?我看着Pool.php,但沒有找到辦法做到這一點。有沒有辦法?
看http://mtdowling.com/blog/2014/09/28/guzzle-ring/,在「發送請求併發」部分。
您可以使用生成器在Guzzle池發送請求時添加請求。
使用while和generator(yield)。
$client = new GuzzleHttp\Client();
$client = new Client();
$requests = function() {
$uris = ['http://base_url'];
$visited_uris = []; // maybe database instead of array
while(len($uris)>0)
yield new Request('GET', array_pop($uris));
}
};
$pool = new Pool($client, $requests(), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
$new_uri = get_new_uri(); // implement function to get new $uri
if(in_array($new_uri, $visited_uris)) {
array_push($uris, $uri);
}
array_push($visited_uris, $uri);
}
]);
$promise = $pool->promise();
$promise->wait();