Skip to content

Instantly share code, notes, and snippets.

@allenpc
Created August 7, 2014 17:57
Show Gist options
  • Save allenpc/7f7277a2685009b38e47 to your computer and use it in GitHub Desktop.
Save allenpc/7f7277a2685009b38e47 to your computer and use it in GitHub Desktop.
Concurrent calls in Python
import eventlet
import time
from eventlet.green import urllib2
urls = [
"http://www.amazon.com/",
"http://www.google.com/",
"http://www.twitter.com/"
]
def fetch(url):
print 'calling %s' % url
start = time.time() * 1000
res = urllib2.urlopen(url).read()
end = time.time() * 1000
print 'completed %(url)s in %(time)d' % {"url": url, "time": (end - start)}
return res
pool = eventlet.GreenPool()
start = time.time() * 1000
for body in pool.imap(fetch, urls):
print 'done'
end = time.time() * 1000
print 'total time: %d' % (end - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment