使用 PHP curl_multi 的单独响应时间

发布于 2024-12-29 07:44:26 字数 1022 浏览 0 评论 0原文

是否可以使用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);

It is possible to record the response times of each process using curl_multi? Here's the code I'm currently using I'm just not sure how to record the response times of each process. Thanks for any help!

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2025-01-05 07:44:26

使用 microtime() 获取 forloop 开始时的时间,然后在末尾使用 microtime() 获取代码结束时的时间,并用开始时的时间减去结束时的时间以获得差值。这就是您的代码运行所需的时间。

编辑:

curl_setopt($ch, CURLOPT_WRITEFUNCTION, ‘read_body’);//sets callback

http://www.php.net/manual/en/function .curl-setopt.php

curl_multi 并行启动进程。因此我之前的建议行不通。解决办法:添加回调!获取所有这些进程启动的微时间,并在回调中记录进程 ID 和签入时间。不幸的是,所有这些进程都将争夺该回调的控制权。我认为解决方案是使用 fork() 创建新进程。这需要做很多工作才能恢复进度条,但如果您有兴趣,这里有一个 php fork() 的链接:
http://php.net/manual/en/function.pcntl-fork.php

Use microtime() to get the time at the start of your forloop and then use microtime() at the end to get the time at the end of your code and subtract the time at the end from the time at the beginning to get the difference. That's how long your code took to run.

EDIT:

curl_setopt($ch, CURLOPT_WRITEFUNCTION, ‘read_body’);//sets callback

http://www.php.net/manual/en/function.curl-setopt.php

curl_multi launches processes in parallel. Therefore my previous suggestion wouldn't work. Solution: add a callback! take the microtime of when all those processes start and in the callback record the process id and the time it checked in. Unfortunately all those processes are going to be battling for control of that callback. I figure the solution to that is using fork() to create new processes. This is getting into quite a lot of work just to get a progress bar back but if you are interested here is a link to the php fork():
http://php.net/manual/en/function.pcntl-fork.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文