doujishan2247 2013-08-20 07:05
浏览 154
已采纳

PHP:http GET / POST以真正的异步方式

Basically, I'm looking for a way to do the following in PHP:

http_get_or_post('an.url', 'or.two');

// Do some work here, not worrying about the http going on in the background.

$r = wait_for_and_get_the_results_of_the_http_requests()

And maybe someone with more curl experience can confirm that curl_multi is what I'm looking for.

From what I gather from http://php.net/manual/en/function.curl-multi-init.php, the sample there might give me what I need:

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch1);

$active = null;
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

// Now, am I free to do some time consuming work here and not worry about
// calling curl_multi_exec every now and then to facilitate the background
// http / socket processes?

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

curl_multi_remove_handle($mh, $ch1);
curl_multi_close($mh);

Now, the main question is, is my understanding of what this would do correct?

1) The first loop will only take care of sending out / writing the requests to the sockets.

2) All of the http / socket stuff will happen in the background after the requests have been sent, leaving me free to do other stuff without having to periodically call curl_multi_exec to make sure that some buffer is not full somewhere and needs a kick to keep going.

3) The second loop will wait for any outstanding response data to arrive and finish reading and processing the responses.

Now, this would still not be fully async - I might become blocked on writing the requests should the socket write buffers fill up, but in my scenario that's not a problem, I'm only worried about having to call curl_multi_exec while I'm doing the other stuff in the middle, just so the whole thing would not freeze until the next chance I get to call curl_multi_exec.

I'm also fine with this being the case for the general scenario of 2k-4k responses, with bigger responses getting stuck doing nothing in the background until I get to the second loop.

Is this how curl_multi works? If not, what can you suggest that will get this done in PHP?

  • 写回答

2条回答 默认 最新

  • dongpao5127 2013-08-20 07:50
    关注

    curl_multi does its work a very small piece at a time. Every call to curl_multi_exec does a little fragment of the transfer operation and once you've called it enough number of times and the data has been sent and received, it is done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog