【问题标题】:Libcurl - how can you explain this behavior curl_multi_pollLibcurl - 你如何解释这种行为 curl_multi_poll
【发布时间】:2022-01-04 14:51:37
【问题描述】:

这是我关于curl_multi_poll的第三个问题,但现在我好像一切都按规矩办了:

#include <iostream>
#include <curl.h>     

int main()
{
    curl_global_init(CURL_GLOBAL_ALL);
    CURLM* CURLM_ = curl_multi_init();
    CURL* CURL_ = curl_easy_init();

    curl_easy_setopt(CURL_, CURLOPT_URL, "https://stackoverflow.com");

    int num_desc_events;
    int running_now;

    curl_multi_add_handle(CURLM_, CURL_);
    curl_multi_perform(CURLM_, &running_now);

    while (1)
    {
        std::cout << "curl_multi_poll_start" << std::endl;

        curl_multi_poll(CURLM_, NULL, 0, 1000000, &num_desc_events);

        std::cout << "curl_multi_poll_awakened" << std::endl;
        std::cout << "num_desc_events:" << num_desc_events << std::endl;

        curl_multi_perform(CURLM_, &running_now);

        std::cout << "curl_multi_perform_start" << std::endl;
        std::cout << "running_now:" << running_now << std::endl;
        
    }

    curl_easy_cleanup(CURL_);
    curl_multi_cleanup(CURLM_);
    curl_global_cleanup();
}

控制台输出如下:

curl_multi_poll_start                   //Begin loop
curl_multi_poll_awakened
num_desc_events:1                       //curl_multi_poll awakened - and the number of sockets on which the event occurred is indicated 
curl_multi_perform_start                //If there are sockets on which events have occurred, I run curl_multi_perform 
running_now:1                           //number of sockets still in operation 
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:0                      //Attention!!! Why does curl_multi_poll wake up if the number of sockets on which events have occurred is 0? 
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:0                         //The same 
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:0                            //the number of sockets in operation is 0. Request completed. 
curl_multi_poll_start                    //curl_multi_poll pending

来自 curl_multi_poll 描述:https://curl.se/libcurl/c/curl_multi_poll.html

完成时,如果 numfds 不为 NULL,则将填充 发生有趣事件的文件描述符总数。

其实问题是:如果socket上没有事件,为什么curl_multi_poll会唤醒两次?

【问题讨论】:

    标签: c++ c libcurl


    【解决方案1】:

    正如文档中所解释的,当 libcurl 有“其他事情”要做时,curl_multi_poll() 可以在没有任何套接字活动的情况下“提前”返回。最值得注意的是基于计时器或超时的事情。

    有时启用CURLOPT_VERBOSE 并观察该输出有助于解释它在特定时间的作用。

    【讨论】:

    • 你是这个意思吗? “...如果多句柄有一个未决的内部超时,其到期时间比 timeout_ms 更短,则将使用更短的时间来确保合理地保持超时准确性。”
    猜你喜欢
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2012-03-16
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多