Skip to content

Instantly share code, notes, and snippets.

@jdmaturen
Created January 15, 2011 22:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdmaturen/781314 to your computer and use it in GitHub Desktop.
Save jdmaturen/781314 to your computer and use it in GitHub Desktop.
from gevent import monkey; monkey.patch_socket()
import gevent
import httplib2
from gevent import queue
from time import time
def worker(q):
h = httplib2.Http(timeout=0.1)
while True:
url, return_q = q.get()
t = time()
print 'getting', url
resp, content = h.request(url, "GET")
return_q.put((url, resp, content, time()-t))
req_q = queue.Queue()
rsp_q = queue.Queue()
urls = ['http://localhost/~jd/sleep.php?ms=400', 'http://localhost/~jd/sleep.php?ms=200',
'http://localhost/~jd/sleep.php?ms=100']
for url in urls:
gevent.spawn(worker, req_q)
for url in urls:
req_q.put((url, rsp_q))
count = len(urls)
while count:
url, resp, content, dt = rsp_q.get()
print url, dt
count -= 1
(jd@orchid) /home/jd> python26 gevent_httplib2_test.py
getting http://localhost/~jd/sleep.php?ms=400
getting http://localhost/~jd/sleep.php?ms=200
getting http://localhost/~jd/sleep.php?ms=100
http://localhost/~jd/sleep.php?ms=100 0.102897882462
http://localhost/~jd/sleep.php?ms=200 0.202988147736
http://localhost/~jd/sleep.php?ms=400 0.411028146744
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment