curl_multi_perform.md (3519B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: curl_multi_perform 5 Section: 3 6 Source: libcurl 7 See-also: 8 - curl_multi_add_handle (3) 9 - curl_multi_cleanup (3) 10 - curl_multi_fdset (3) 11 - curl_multi_info_read (3) 12 - curl_multi_init (3) 13 - curl_multi_wait (3) 14 - libcurl-errors (3) 15 Protocol: 16 - All 17 Added-in: 7.9.6 18 --- 19 20 # NAME 21 22 curl_multi_perform - run all transfers until it would block 23 24 # SYNOPSIS 25 26 ~~~c 27 #include <curl/curl.h> 28 29 CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles); 30 ~~~ 31 32 # DESCRIPTION 33 34 This function performs transfers on all the added handles that need attention 35 in a non-blocking fashion. The easy handles have previously been added to the 36 multi handle with curl_multi_add_handle(3). 37 38 When an application has found out there is data available for the multi_handle 39 or a timeout has elapsed, the application should call this function to 40 read/write whatever there is to read or write right now etc. 41 curl_multi_perform(3) returns as soon as the reads/writes are done. This 42 function does not require that there actually is any data available for 43 reading or that data can be written, it can be called just in case. It stores 44 the number of handles that still transfer data in the second argument's 45 integer-pointer. 46 47 If the amount of *running_handles* is changed from the previous call (or 48 is less than the amount of easy handles you have added to the multi handle), 49 you know that there is one or more transfers less "running". You can then call 50 curl_multi_info_read(3) to get information about each individual 51 completed transfer, and that returned info includes CURLcode and more. If an 52 added handle fails quickly, it may never be counted as a running_handle. You 53 could use curl_multi_info_read(3) to track actual status of the added 54 handles in that case. 55 56 When *running_handles* is set to zero (0) on the return of this function, 57 there is no longer any transfers in progress. 58 59 When this function returns error, the state of all transfers are uncertain and 60 they cannot be continued. curl_multi_perform(3) should not be called 61 again on the same multi handle after an error has been returned, unless first 62 removing all the handles and adding new ones. 63 64 # %PROTOCOLS% 65 66 # EXAMPLE 67 68 ~~~c 69 int main(void) 70 { 71 int still_running; 72 CURLM *multi = curl_multi_init(); 73 CURL *curl = curl_easy_init(); 74 if(curl) { 75 curl_multi_add_handle(multi, curl); 76 do { 77 CURLMcode mc = curl_multi_perform(multi, &still_running); 78 79 if(!mc && still_running) 80 /* wait for activity, timeout or "nothing" */ 81 mc = curl_multi_poll(multi, NULL, 0, 1000, NULL); 82 83 if(mc) { 84 fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc); 85 break; 86 } 87 88 /* if there are still transfers, loop */ 89 } while(still_running); 90 } 91 } 92 ~~~ 93 94 # %AVAILABILITY% 95 96 # RETURN VALUE 97 98 This function returns a CURLMcode indicating success or error. 99 100 CURLM_OK (0) means everything was OK, non-zero means an error occurred, see 101 libcurl-errors(3). 102 103 This function returns errors regarding the whole multi stack. Problems on 104 individual transfers may have occurred even when this function returns 105 *CURLM_OK*. Use curl_multi_info_read(3) to figure out how individual transfers 106 did. 107 108 # TYPICAL USAGE 109 110 Most applications use curl_multi_poll(3) to make libcurl wait for 111 activity on any of the ongoing transfers. As soon as one or more file 112 descriptor has activity or the function times out, the application calls 113 curl_multi_perform(3).