Created
January 31, 2014 11:36
-
-
Save icsaas/8730499 to your computer and use it in GitHub Desktop.
gevent example
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 gevent | |
from gevent import monkey | |
#patches stdlib (including socket and ssl modules) to cooperate with other greenlets | |
monkey.patch_all() | |
import urllib2 | |
urls=['http://baidu.com','http://cqu.edu.cn','http://www.python.org'] | |
def print_head(url): | |
print 'Starting %s' %url | |
data=urllib2.urlopen(url).read() | |
print '%s: %s bytes: %r' %(url,len(data),data[:50]) | |
jobs=[gevent.spawn(print_head,url) for url in urls] | |
gevent.joinall(jobs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment