Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created December 4, 2014 22:16
Show Gist options
  • Save alejandrobernardis/721291dd39c41475e458 to your computer and use it in GitHub Desktop.
Save alejandrobernardis/721291dd39c41475e458 to your computer and use it in GitHub Desktop.
Nested coroutines
from tornado import gen, ioloop
from tornado.httpclient import AsyncHTTPClient
@gen.coroutine
def worker():
sites = ['http://google.com', 'http://clarin.com', 'http://lanacion.com']
@gen.coroutine
def _bulk():
if not sites:
raise gen.Return(False)
site = sites.pop()
response = yield AsyncHTTPClient().fetch(site)
print 'Code(%s): %s' % (response.code, site)
raise gen.Return(True)
while True:
result = yield _bulk()
if not result:
break
raise gen.Return(sites)
@gen.coroutine
def run():
try:
x = yield worker()
print x
except Exception, e:
print e
ioloop.IOLoop.instance().stop()
if __name__ == '__main__':
run()
ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment