Skip to content

Instantly share code, notes, and snippets.

@rbranson
Created August 29, 2013 05:19
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 rbranson/6374514 to your computer and use it in GitHub Desktop.
Save rbranson/6374514 to your computer and use it in GitHub Desktop.
Illustrates how to launch & yield to gevent greenlets from a non-gevent context.
import gevent
from gevent import httplib
def fetcher():
print "fetcher: Fetching..."
conn = httplib.HTTPConnection("www.google.com")
conn.request("GET", "/")
res = conn.getresponse()
output = res.read()
print "fether: Received %s bytes" % len(output)
print "main: Spawning..."
g = gevent.spawn(fetcher)
print "main: Spawned."
gevent.sleep(0)
print "main: Sleeped."
gevent.joinall([g])
print "main: Joined."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment