Home > Software engineering >  Libcurl - What does "curl_multi_perform"
Libcurl - What does "curl_multi_perform"

Time:12-19

I'm trying to understand how curl_multi_perform works. The documentation says that:

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.

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

OK, I found two simple usage examples - "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);

-Firstly:

  • 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.

Nothing is clear.

  • Secondly:

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

Why is one call not enough ?

CodePudding user response:

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

Programmers don't call functions. Programmers write programs that tell the computer what to do. So this means that the programmer should write code that tells the application to call the function.

  • 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.

Either order works. As the documentation says:

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.

If there's nothing available, it will simply return immediately, updating transfers_running.

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

Because multiple transfers are in progress. curl_multi_wait() returns as soon as data is available on any of them. After you process that data, you need to continue waiting for other transfers.

Also, this doesn't wait for transfers to be complete, it processes partial data as it arrives. So you have to keep calling it until you've sent or received everything.

  • Related