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