Created
          December 4, 2014 22:16 
        
      - 
      
- 
        Save alejandrobernardis/721291dd39c41475e458 to your computer and use it in GitHub Desktop. 
    Nested coroutines
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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