Created
August 7, 2014 17:57
-
-
Save allenpc/7f7277a2685009b38e47 to your computer and use it in GitHub Desktop.
Concurrent calls in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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