xref: /curl/docs/libcurl/curl_multi_perform.md (revision 547d6004)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_multi_perform
5Section: 3
6Source: libcurl
7See-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)
15Protocol:
16  - All
17Added-in: 7.9.6
18---
19
20# NAME
21
22curl_multi_perform - run all transfers until it would block
23
24# SYNOPSIS
25
26~~~c
27#include <curl/curl.h>
28
29CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles);
30~~~
31
32# DESCRIPTION
33
34This function performs transfers on all the added handles that need attention
35in a non-blocking fashion. The easy handles have previously been added to the
36multi handle with curl_multi_add_handle(3).
37
38When an application has found out there is data available for the multi_handle
39or a timeout has elapsed, the application should call this function to
40read/write whatever there is to read or write right now etc.
41curl_multi_perform(3) returns as soon as the reads/writes are done. This
42function does not require that there actually is any data available for
43reading or that data can be written, it can be called just in case. It stores
44the number of handles that still transfer data in the second argument's
45integer-pointer.
46
47If the amount of *running_handles* is changed from the previous call (or
48is less than the amount of easy handles you have added to the multi handle),
49you know that there is one or more transfers less "running". You can then call
50curl_multi_info_read(3) to get information about each individual
51completed transfer, and that returned info includes CURLcode and more. If an
52added handle fails quickly, it may never be counted as a running_handle. You
53could use curl_multi_info_read(3) to track actual status of the added
54handles in that case.
55
56When *running_handles* is set to zero (0) on the return of this function,
57there is no longer any transfers in progress.
58
59When this function returns error, the state of all transfers are uncertain and
60they cannot be continued. curl_multi_perform(3) should not be called
61again on the same multi handle after an error has been returned, unless first
62removing all the handles and adding new ones.
63
64# %PROTOCOLS%
65
66# EXAMPLE
67
68~~~c
69int 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
98CURLMcode type, general libcurl multi interface error code.
99
100This function returns errors regarding the whole multi stack. Problems on
101individual transfers may have occurred even when this function returns
102*CURLM_OK*. Use curl_multi_info_read(3) to figure out how individual
103transfers did.
104
105# TYPICAL USAGE
106
107Most applications use curl_multi_poll(3) to make libcurl wait for
108activity on any of the ongoing transfers. As soon as one or more file
109descriptor has activity or the function times out, the application calls
110curl_multi_perform(3).
111