Jim*_* M. 5 php asynchronous curl-multi
是否可以使用curl_multi记录每个进程的响应时间?这是我当前使用的代码,我只是不确定如何记录每个进程的响应时间。谢谢你的帮助!
do
{
$execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
// Loop and continue processing the request
while ($runningHandles && $execReturnValue == CURLM_OK)
{
// Wait forever for network
$numberReady = curl_multi_select($mh);
if ($numberReady != -1)
{
// Pull in any new data, or at least handle timeouts
do
{
$execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
}
}
//End Run
//Get Data and Close
foreach($ping as $cid=>$p)
{
$curlError = curl_error($c[$cid]);
if($curlError == "")
{
$curl_data[$cid] = curl_multi_getcontent($c[$cid]);
}
else
{
//email me!
}
curl_multi_remove_handle($mh,$c[$cid]);
curl_close($c[$cid]);
}
curl_multi_close($mh);
Run Code Online (Sandbox Code Playgroud)
使用 microtime() 获取 forloop 开始时的时间,然后在末尾使用 microtime() 获取代码结束时的时间,并用开始时的时间减去结束时的时间以获得差值。这就是您的代码运行所需的时间。
\n\n编辑:
\n\ncurl_setopt($ch, CURLOPT_WRITEFUNCTION, \xe2\x80\x98read_body\xe2\x80\x99);//sets callback\n
Run Code Online (Sandbox Code Playgroud)\n\nhttp://www.php.net/manual/en/function.curl-setopt.php
\n\ncurl_multi 并行启动进程。因此我之前的建议行不通。解决办法:添加回调!获取所有这些进程启动的微时间,并在回调中记录进程 ID 和签入时间。不幸的是,所有这些进程都将争夺该回调的控制权。我认为解决方案是使用 fork() 创建新进程。这需要做很多工作才能恢复进度条,但如果您有兴趣,这里有一个 php fork() 的链接:\n http://php.net/manual/en/function.pcntl- fork.php
\n