Last active
          August 29, 2015 14:08 
        
      - 
      
- 
        Save alejandrobernardis/243900a045e568893895 to your computer and use it in GitHub Desktop. 
    Tornado 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, HTTPRequest | |
| from tornado.web import HTTPError | |
| AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient") | |
| class CoroutineModel(object): | |
| @gen.coroutine | |
| def send_ok(self): | |
| try: | |
| request = HTTPRequest('https://google.com') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| except Exception, e: | |
| response = e | |
| raise gen.Return(response) | |
| @gen.coroutine | |
| def send_error(self): | |
| try: | |
| request = HTTPRequest('https://google.com', 'POST', body='a=1') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| except Exception, e: | |
| response = e | |
| raise gen.Return(response) | |
| @gen.coroutine | |
| def run_coroutine(): | |
| print 'COROUTINE' | |
| print '*'*80 | |
| c = CoroutineModel() | |
| _ok = yield c.send_ok() | |
| print 'OK:', _ok | |
| _error = yield c.send_error() | |
| print '\nERROR:', _error | |
| print '\n'*2 | |
| class engine(object): | |
| @gen.engine | |
| def send_ok(self, callback): | |
| request = HTTPRequest('https://google.com') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| callback(response) | |
| @gen.engine | |
| def send_error(self, callback): | |
| request = HTTPRequest('https://google.com', 'POST', body='a=1') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| callback(response) | |
| @gen.engine | |
| def run_engine(): | |
| print 'ENGINE' | |
| print '*'*80 | |
| def _on_complete(response): | |
| print 'OK:', response | |
| e = engine() | |
| e.send_ok(_on_complete) | |
| try: | |
| e.send_error(_on_complete) | |
| except Exception, e: | |
| print '\nERROR:', e | |
| if __name__ == '__main__': | |
| run_coroutine() | |
| run_engine() | |
| ioloop.IOLoop.instance().start() | 
  
    
      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, autoreload | |
| from tornado.httpclient import AsyncHTTPClient, HTTPRequest | |
| from tornado.web import HTTPError, RequestHandler, Application | |
| AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient") | |
| @gen.coroutine | |
| def fetch(): | |
| request = HTTPRequest('https://google.com', 'POST', body='a=1') | |
| raise ValueError('1... 2... 3... ERROR.') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| raise gen.Return(response) | |
| class MainHandler(RequestHandler): | |
| @gen.coroutine | |
| def get(self): | |
| self.add_header('Content-Type', 'text/plain; charset=utf-8') | |
| self.write('Write a message...') | |
| request = HTTPRequest('https://google.com') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| self.write(response.body) | |
| self.finish() | |
| class MainErrorHandler(RequestHandler): | |
| @gen.coroutine | |
| def get(self): | |
| self.add_header('Content-Type', 'text/plain; charset=utf-8') | |
| self.write('Write a message...\n') | |
| try: | |
| request = HTTPRequest('https://google.com', 'POST') | |
| response = yield AsyncHTTPClient().fetch(request) | |
| self.write(response.body) | |
| except Exception, e: | |
| self.write('E: %s' % str(e)) | |
| self.finish() | |
| @gen.coroutine | |
| def post(self): | |
| self.add_header('Content-Type', 'text/plain; charset=utf-8') | |
| self.write('Write a message...\n') | |
| try: | |
| response = yield fetch() | |
| self.write(response.body) | |
| except Exception, e: | |
| self.write('E: %s' % str(e)) | |
| self.finish() | |
| application = Application([ | |
| (r"/", MainHandler), | |
| (r"/try", MainErrorHandler), | |
| ]) | |
| if __name__ == "__main__": | |
| application.listen(8000) | |
| ioloop.IOLoop.instance().start() | |
| autoreload.start() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment