gpt4 book ai didi

php - 如何向 Guzzle 池动态添加额外请求?

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:46 26 4
gpt4 key购买 nike

我正在使用 Guzzle ( http://guzzlephp.org ) 获取大量 url (~300k)。 url 是从 Elastic Search 实例中检索的,我想继续将 url 添加到池中,以便池保持相当小而不是一次添加它们。

这可能吗?我查看了 Pool.php,但没有找到执行此操作的方法。有办法吗?

最佳答案

使用 while 和生成器(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();

关于php - 如何向 Guzzle 池动态添加额外请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26716286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com