Skip to content

Commit

Permalink
Added a function that allows a maximum number of requests per second.
Browse files Browse the repository at this point in the history
  • Loading branch information
conradlee committed Jun 25, 2011
1 parent 149c051 commit 73f8e34
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions pyparallelcurl.py 100755 → 100644
Expand Up @@ -56,12 +56,17 @@ class ParallelCurl:
outstanding_requests = {}
multi_handle = None

def __init__(self, in_max_requests = 10, in_options = {}):
def __init__(self, in_max_requests = 10, in_options = {}, max_requests_per_second = 5):
self.max_requests = in_max_requests
self.options = in_options

self.outstanding_requests = {}
self.multi_handle = pycurl.CurlMulti()

# Variables for rate limiting
self.max_requests_per_second = max_requests_per_second
self.last_used_second = 0
self.requests_this_second = 0

# Ensure all the requests finish nicely
def __del__(self):
Expand Down Expand Up @@ -97,7 +102,13 @@ def startrequest(self, url, callback, user_data = {}, post_fields=None):
ch.setopt(pycurl.POSTFIELDS, post_fields)

self.multi_handle.add_handle(ch)


if int(time.time()) == self.last_used_second:
self.requests_this_second += 1
else:
self.last_used_second = int(time.time())
self.requests_this_second = 1

self.outstanding_requests[ch] = {
'handle': ch,
'result_buffer': result_buffer,
Expand Down Expand Up @@ -170,10 +181,17 @@ def checkforcompletedrequests(self):
break

# Blocks until there's less than the specified number of requests outstanding
def waitforoutstandingrequeststodropbelow(self, max):
def waitforoutstandingrequeststodropbelow(self, max_simultaneous, max_per_second):
while True:

if self.requests_this_second > max_per_second:
while int(time.time()) == self.last_used_second:
time.sleep(0.01)
self.requests_this_second = 0
self.last_used_second = int(time.time())

self.checkforcompletedrequests()
if len(self.outstanding_requests) < max:
if len(self.outstanding_requests) < max_simultaneous:
break

time.sleep(0.01)

0 comments on commit 73f8e34

Please sign in to comment.