CurlMulti Object

class pycurl.CurlMulti(close_handles=False) New CurlMulti object

Creates a new CurlMulti Object which corresponds to a CURLM handle in libcurl.

The CurlMulti object can be used as a context manager. Exiting the context calls close().

Example:

with pycurl.CurlMulti(close_handles=True) as m:
    m.add_handle(curl)
    # perform multi operations
# easy handles have been removed and closed
Parameters:

close_handles (bool) –

If False (default), easy handles added to the multi handle are removed from the multi handle when close() is called or when exiting the context manager, but remain open and must be managed by the caller.

If True, easy handles are removed from the multi handle when close() is called or when exiting the context manager, and are then automatically closed.

In all cases, easy handles are not closed when they are removed individually from the multi handle.

CurlMulti objects have the following methods:

close() None

Corresponds to curl_multi_cleanup in libcurl. This method is automatically called by pycurl when a CurlMulti object no longer has any references to it, but can also be called explicitly.

It removes all easy handles from the multi handle before closing the multi handle.

If the CurlMulti was constructed with close_handles=True, the removed easy handles are also closed after removal. Otherwise, they remain open.

close() may not be called while perform() or socket_action() is on the stack (for example, from inside M_SOCKETFUNCTION or M_TIMERFUNCTION); doing so raises pycurl.error.

add_handle(Curl object) None

Corresponds to curl_multi_add_handle in libcurl. This method adds an existing and valid Curl object to the CurlMulti object.

Changed in version 7.43.0.2: add_handle now ensures that the Curl object is not garbage collected while it is being used by a CurlMulti object. Previously application had to maintain an outstanding reference to the Curl object to keep it from being garbage collected.

remove_handle(Curl object) None

Corresponds to curl_multi_remove_handle in libcurl. This method removes an existing and valid Curl object from the CurlMulti object.

perform() tuple of status and the number of active Curl objects

Corresponds to curl_multi_perform in libcurl.

This method raises an exception if curl_multi_perform returns a value other than CURLM_OK.

socket_action(sock_fd, ev_bitmask)

Returns result from doing a socket_action() on the curl multi file descriptor with the given timeout. Corresponds to curl_multi_socket_action in libcurl.

PycURL exposes the relevant constants as pycurl.CSELECT_IN, CSELECT_OUT, CSELECT_ERR (for ev_bitmask) and pycurl.SOCKET_TIMEOUT (for sock_fd); refer to the curl_multi_socket_action docs for their meaning.

The return value is a two-element tuple. The first element is the return value of the underlying curl_multi_socket_action function, and it is always zero (CURLE_OK) because any other return value would cause socket_action to raise an exception. The second element is the number of running easy handles within this multi handle. When the number of running handles reaches zero, all transfers have completed. Note that if the number of running handles has decreased by one compared to the previous invocation, this is not mean the handle corresponding to the sock_fd provided as the argument to this function was the completed handle.

socket_all() tuple

Returns result from doing a socket_all() on the curl multi file descriptor with the given timeout.

setopt(option, value) None

Set curl multi option. Corresponds to curl_multi_setopt in libcurl.

option specifies which option to set. PycURL defines constants corresponding to CURLMOPT_* constants in libcurl, except that the CURLMOPT_ prefix is replaced with M_ prefix. For example, CURLMOPT_PIPELINING is exposed in PycURL as pycurl.M_PIPELINING. For convenience, CURLMOPT_* constants are also exposed on CurlMulti objects:

import pycurl
m = pycurl.CurlMulti()
m.setopt(pycurl.M_PIPELINING, 1)
# Same as:
m.setopt(m.M_PIPELINING, 1)

value specifies the value to set the option to. Different options accept values of different types:

  • Options specified by curl_multi_setopt as accepting 1 or an integer value accept Python integers and booleans:

    m.setopt(pycurl.M_PIPELINING, True)
    m.setopt(pycurl.M_PIPELINING, 1)
    
  • *FUNCTION options accept a function. Supported callbacks are CURLMOPT_SOCKETFUNCTION and CURLMOPT_TIMERFUNCTION; see the SOCKETFUNCTION and TIMERFUNCTION sections of the callbacks page. CURLMOPT_SOCKETDATA and CURLMOPT_TIMERDATA are reserved by PycURL (set internally to the CurlMulti instance) and cannot be set from Python.

Raises TypeError when the option value is not of a type accepted by the respective option, and pycurl.error exception when libcurl rejects the option or its value.

fdset() tuple of lists with active file descriptors, readable, writeable, exceptions

Returns a tuple of three lists that can be passed to the select.select() method.

Corresponds to curl_multi_fdset in libcurl. This method extracts the file descriptor information from a CurlMulti object. The returned lists can be used with the select module to poll for events.

Example usage:

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "https://curl.haxx.se")
m = pycurl.CurlMulti()
m.add_handle(c)
_, num_handles = m.perform()
while num_handles:
    apply(select.select, m.fdset() + (1,))
    _, num_handles = m.perform()
select([timeout]) number of ready file descriptors or 0 on timeout

Returns result from doing a select() on the curl multi file descriptor with the given timeout.

This is a convenience function which simplifies the combined use of fdset() and the select module.

Example usage:

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "https://curl.haxx.se")
m = pycurl.CurlMulti()
m.add_handle(c)
_, num_handles = m.perform()
while num_handles:
    ret = m.select(1.0)
    if ret == 0:  continue
    _, num_handles = m.perform()
info_read([max_objects]) -> tuple(number of queued messages, a list of successful objects, a list of failed objects)

Corresponds to the curl_multi_info_read function in libcurl.

This method extracts at most max messages from the multi stack and returns them in two lists. The first list contains the handles which completed successfully and the second list contains a tuple (curl object, curl error number, curl error message) for each failed curl object. The curl error message is returned as a Python string which is decoded from the curl error string using the surrogateescape error handler. The number of queued messages after this method has been called is also returned.

timeout() int

Returns how long to wait for action before proceeding, in milliseconds, or -1 if libcurl has no timeout currently set. Corresponds to curl_multi_timeout in libcurl.

assign(sock_fd, object) None

Creates an association in the multi handle between the given socket and a private object in the application. Corresponds to curl_multi_assign in libcurl. The multi handle keeps a strong reference to the assigned object.

assign() may be called from inside the M_SOCKETFUNCTION callback; this is the typical place to attach per-socket state. The new value takes effect for future callbacks for that socket – the socketp argument already passed to the in-flight callback is not mutated.

If object is None, clears any association for the socket. For convenience, pycurl.CurlMulti.unassign() is equivalent to multi.assign(sock_fd, None).

unassign(sock_fd) None

Clears the association in the multi handle for the given socket, releasing the previously assigned object.

multi.unassign(sock_fd) is equivalent to multi.assign(sock_fd, None). Like assign(), it may be called from inside the M_SOCKETFUNCTION.