gpt4 book ai didi

php - curl_multi_select 总是阻塞超时值

转载 作者:搜寻专家 更新时间:2023-10-31 20:48:39 25 4
gpt4 key购买 nike

运行时Example #1 from PHP在 Windows XP PHP 5.3.5 上,curl_multi_select() 行将始终在指定的超时时间内阻塞(如果为空,它将阻塞 1 秒,如果我指定 5 秒超时,它将阻塞 5 sec) 而不管获取内容所花费的时间。我怀疑它与 this bug 有关.

问题是:最好的解决方法是什么?我能想到的最好办法是摆脱 curl_multi_select()usleep(x) 作为节省一些周期的方法。

最佳答案

只要您能忍受 1 秒的阻塞,这可能会有所帮助。

manual page for curl_multi_select上有评论提到此阻塞会持续到至少一个连接已完成或 $timeout 秒,以先发生者为准。他们还写道,对 curl_multi_select 的调用应该被包装:

private function full_curl_multi_exec($mh, &$still_running) {
do {
$rv = curl_multi_exec($mh, $still_running);
} while ($rv == CURLM_CALL_MULTI_PERFORM);
return $rv;
}

然后修改检查运行句柄的循环:

// execute the handles
$still_running = null;
$this->full_curl_multi_exec($mh, $still_running);

// check whether the handles have finished
do { // "wait for completion"-loop
curl_multi_select($mh); // non-busy (!) wait for state change
$this->full_curl_multi_exec($mh, $still_running); // get new state
while ($info = curl_multi_info_read($mh)) {
// process completed request (e.g. curl_multi_getcontent($info['handle']))
}
} while ($still_running);

在此修改之前,由于 this bug in PHP 5.3.18,使用 PHP 5.4 测试的代码无法在运行 PHP 5.3.20 的亚马逊实例上运行。 ,这会导致对 curl_multi_select() 的调用永远不会返回除 -1

以外的任何内容

我现在能够在不到 30 秒的时间内使用 200 个句柄从近 1,300 个 URL 中检索数据。

关于php - curl_multi_select 总是阻塞超时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474325/

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