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

时间:2018-02-16 16:00:46

标签: php curl

以下是我的工作代码示例。只需添加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";

现在使用for循环调用$multi->addCurl。当我添加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秒内发送,而循环开始工作。

使用较少数量的句柄,在curl_multi_select未返回curl_multi_exec之前的循环中调用curl_multi_select-1已解决此问题,但这非常不可靠。它在另一台计算机上根本不起作用,有时会遇到curl_multi_select总是返回-1

1 个答案:

答案 0 :(得分:1)

我找到了一个黑客解决方案。这是使用pretransfer_time检查curl_getinfo

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

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