Libcurl - 你如何解释这种行为 curl_multi_poll

Libcurl - how can you explain this behavior curl_multi_poll

这是我关于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://whosebug.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

On completion, if numfds is non-NULL, it will be populated with the total number of file descriptors on which interesting events occurred.

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

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

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