CurlMulti Object

class pycurl.CurlMulti → New CurlMulti object

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

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.

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.

IMPORTANT NOTE: add_handle does not implicitly add a Python reference to the Curl object (and thus does not increase the reference count on the Curl object).

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.

IMPORTANT NOTE: remove_handle does not implicitly remove a Python reference from the Curl object (and thus does not decrease the reference count on the Curl object).

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

Corresponds to curl_multi_perform in libcurl.

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, long integers (on Python 2.x) and booleans:

    m.setopt(pycurl.M_PIPELINING, True)
    m.setopt(pycurl.M_PIPELINING, 1)
    # Python 2.x only:
    m.setopt(pycurl.M_PIPELINING, 1L)
    
  • *FUNCTION options accept a function. Supported callbacks are CURLMOPT_SOCKETFUNCTION AND CURLMOPT_TIMERFUNCTION. Please refer to the PycURL test suite for examples on using the callbacks.

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)
while 1:
    ret, num_handles = m.perform()
    if ret != pycurl.E_CALL_MULTI_PERFORM: break
while num_handles:
    apply(select.select, m.fdset() + (1,))
    while 1:
        ret, num_handles = m.perform()
        if ret != pycurl.E_CALL_MULTI_PERFORM: break
select([timeout]) → number of ready file descriptors or -1 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)
while 1:
    ret, num_handles = m.perform()
    if ret != pycurl.E_CALL_MULTI_PERFORM: break
while num_handles:
    ret = m.select(1.0)
    if ret == -1:  continue
    while 1:
        ret, num_handles = m.perform()
        if ret != pycurl.E_CALL_MULTI_PERFORM: break
info_read([max_objects]) -> tuple(number of queued messages, a list of successful objects, a list of failed objects)

Returns a tuple (number of queued handles, [curl 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 number of queued messages after this method has been called is also returned.

timeout() → int

Returns how long to wait for action before proceeding. Corresponds to curl_multi_timeout in libcurl.

assign(sockfd, 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.