Skip to content

Instantly share code, notes, and snippets.

@icsaas
Created January 31, 2014 11:36
Show Gist options
  • Save icsaas/8730499 to your computer and use it in GitHub Desktop.
Save icsaas/8730499 to your computer and use it in GitHub Desktop.
gevent example
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