Libcurl - "curl_multi_perform" 的作用

Libcurl - What does "curl_multi_perform"

我正在尝试了解 curl_multi_perform 的工作原理。 文档说:

This function performs transfers on all the added handles that need attention in an non-blocking fashion. The easy handles have previously been added to the multi handle with curl_multi_add_handle.

When an application has found out there's data available for the multi_handle or a timeout has elapsed, the application should call this function to read/write whatever there is to read or write right now etc.

问题一:“应用程序应该调用”是什么意思?应用程序如何导致某些事情?您是说程序员应该调用吗?

好的,我找到了两个简单的用法示例 - "curl_multi_perform":

1 - https://everything.curl.dev/libcurl/drive/multi

int transfers_running;
do {
   curl_multi_wait ( multi_handle, NULL, 0, 1000, NULL);
   curl_multi_perform ( multi_handle, &transfers_running );
} while (transfers_running);

2 - enter link description here

int still_running;
do {
  CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
 
  if(!mc && still_running)
    /* wait for activity, timeout or "nothing" */
    mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
 
  if(mc) {
    fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc);
    break;
  }
 
/* if there are still transfers, loop! */
} while(still_running);

-首先:

什么都不清楚。

为什么我需要循环调用 curl_multi_perform ??没看懂。

为什么一次调用不够?

Question 1: What does the "application should call" mean? How can an application cause something? Did you mean the programmer should call ?

程序员不调用函数。程序员编写告诉计算机做什么的程序。所以这意味着程序员应该编写告诉应用程序调用函数的代码。

  • in the first example curl_multi_perform is called after curl_multi_wait.
  • in the second example curl_multi_perform is called before curl_multi_wait.

两种顺序都行。正如文档所说:

This function does not require that there actually is any data available for reading or that data can be written, it can be called just in case.

如果没有可用的,它会立即return,更新transfers_running

Why do I need to call curl_multi_perform in a loop ?? I do not understand.

因为正在进行多项传输。 curl_multi_wait() returns 一旦有任何数据可用。处理完该数据后,您需要继续等待其他传输。

此外,这不会等待传输完成,它会在数据到达时处理部分数据。所以你必须一直调用它,直到你发送或接收所有内容。