gpt4 book ai didi

PHP 7 - 在等待 curl 时执行代码

转载 作者:可可西里 更新时间:2023-11-01 12:59:36 25 4
gpt4 key购买 nike

以下是我的工作代码示例。只需添加您自己的 sleep.php 即可 sleep($_GET['sleep']);

class MultiCurl {
private $mc;
private $running;
private $execStatus;

public function __construct() {
$this->mc = curl_multi_init();
}

public function addCurl($ch) {
$code = curl_multi_add_handle($this->mc, $ch);

if ($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) {
do {
$this->execStatus = curl_multi_exec($this->mc, $this->running);
} while ($this->execStatus === CURLM_CALL_MULTI_PERFORM);

return $this->getKey($ch);
}
return null;
}

public function getNextResult() {
if ($this->running) {
while ($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) {
usleep(2500);
curl_multi_exec($this->mc, $this->running);

$responses = $this->readResponses();
if ($responses !== null) {
return $responses;
}
}
} else {
return $this->readResponses();
}

return null;
}

private function readResponses() {
$responses = [];
while ($done = curl_multi_info_read($this->mc)) {
$key = $this->getKey($done['handle']);

$done['response'] = curl_multi_getcontent($done['handle']);
$done['info'] = curl_getinfo($done['handle']);
$error = curl_error($done['handle']);
if ($error) {
$done['error'] = $error;
}

$responses[$key] = $done;

curl_multi_remove_handle($this->mc, $done['handle']);
curl_close($done['handle']);
}

if (!empty($responses)) {
return $responses;
}

return null;
}

private function getKey($ch) {
return (string)$ch;
}
}

function getHandle($url) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5
]);
return $ch;
}

$totalTime = microtime(true);

$multi = new MultiCurl();

$keys = [];
$addCurlHandles = microtime(true);
$keys[] = $multi->addCurl(getHandle('http://localhost/sleep.php?sleep=5'));
for ($i = 0; $i < 5; $i++) {
$keys[] = $multi->addCurl(getHandle('http://localhost/sleep.php?sleep=' . random_int(1, 4)));
}
echo 'Add curl handles: ' . (microtime(true) - $addCurlHandles) . "\n";

/**/
$loop = microtime(true);
while (microtime(true) - $loop < 2) {
usleep(100);
}
echo 'Loop: ' . (microtime(true) - $loop) . "\n";
/**/

$getResults = microtime(true);
while ($result = $multi->getNextResult()) {
foreach ($result as $key => $response) {
echo $response['response'] . "\n";
}
}
echo 'Get results: ' . (microtime(true) - $getResults) . "\n";

echo 'Total time: ' . (microtime(true) - $totalTime) . "\n";

现在尝试调用 $multi->addCurlfor 循环。当我添加 4 个句柄时,输出类似于

Add curl handles: 0.0007021427154541
Loop: 2.0000491142273
Slept 1
Slept 3
Slept 3
Slept 4
Slept 5
Get results: 5.0043671131134
Total time: 7.0052678585052

但是当我加 5 或更多时,输出是

Add curl handles: 0.0014941692352295
Loop: 2.00008893013
Slept 1
Slept 2
Slept 4
Slept 4
Slept 4
Slept 5
Get results: 3.0007629394531
Total time: 5.0025300979614

如您所见,后者完成更多工作但完成速度更快,因为 5 秒休眠请求实际上是在 2 秒 while 循环开始工作之前发送的。

在句柄数量较少的情况下,循环调用curl_multi_selectcurl_multi_exec 直到curl_multi_select 不返回-1 已经解决了这个问题,但它非常不可靠。它在另一台计算机上根本不起作用,有时会卡在 curl_multi_select 总是返回 -1 的情况下。

最佳答案

我找到了一个 hack 解决方案。它是使用curl_getinfo检查pretransfer_time

我已经在github上发布了源代码:https://github.com/rinu/multi-curl

更好、更清洁的解决方案仍然非常受欢迎。

关于PHP 7 - 在等待 curl 时执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48830527/

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