Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
100644 62 lines (41 sloc) 3.005 kb
149c051 @petewarden Initial import
1 ParallelCurl
2 ~~~~~~~~~~~~~~~
4 This module provides an easy-to-use interface to allow you to run multiple CURL url fetches in
5 parallel in Python, without threads.
7 To test it, go to the command line, cd to this folder and run
9 ./
11 This should run 100 searches through Google's API, printing the results. To see what sort of
12 performance difference running parallel requests gets you, try altering the default of 10 requests
13 running in parallel using the optional script argument, and timing how long each takes:
15 time ./ 1
16 time ./ 20
18 The first only allows one request to run at once, serializing the calls. I see this taking around
19 100 seconds. The second run has 20 in flight at a time, and takes 11 seconds! Be warned though,
20 it's possible to overwhelm your target if you fire too many requests at once. You may end up
21 with your IP banned from accessing that server, or hit other API limits.
23 The class is designed to make it easy to run multiple curl requests in parallel, rather than
24 waiting for each one to finish before starting the next. Under the hood it uses curl_multi_exec
25 but since I find that interface painfully confusing, I wanted one that corresponded to the tasks
26 that I wanted to run.
28 To use it, easy_install pycurl, import pyparallelcurl, then create the ParallelCurl object:
30 parallelcurl = ParallelCurl(10)
32 The first argument to the constructor is the maximum number of outstanding fetches to allow
33 before blocking to wait for one to finish. You can change this later using setmaxrequests()
34 The second optional argument is an array of curl options in the format used by curl_setopt_array()
36 Next, start a URL fetch:
38 parallelcurl.startRequest('', on_request_done, {'somekey':'somevalue'})
40 The first argument is the address that should be fetched
41 The second is the callback function that will be run once the request is done
42 The third is a 'cookie', that can contain arbitrary data to be passed to the callback
44 This startRequest call will return immediately, as long as less than the maximum number of
45 requests are outstanding. Once the request is done, the callback function will be called, eg:
47 on_request_done(content, '', ch, {'somekey':'somevalue'})
49 The callback should take four arguments. The first is a string containing the content found at
50 the URL. The second is the original URL requested, the third is the curl handle of the request that
51 can be queried to get the results, and the fourth is the arbitrary 'cookie' value that you
52 associated with this object. This cookie contains user-defined data.
54 Since you may have requests outstanding at the end of your script, you *MUST* call
56 parallelcurl.finishallrequests()
58 before you exit. If you don't, the final requests may be left unprocessed! This is actually also
59 called in the destructor of the class, but it's definitely best practice to call this explictly.
61 By Pete Warden <>, freely reusable, see for more
Something went wrong with that request. Please try again.