Last active
May 31, 2018 15:38
-
-
Save apast/2257634371bc064b4613f7cb675983db to your computer and use it in GitHub Desktop.
This file contains 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.web import RequestHandler | |
from tornado.concurrent import coroutine | |
from tornado.gen import return_future | |
class SuggiroCafeDetail(RequestHandler): | |
@coroutine # isto marca que esta função utilizará o IOLoop (async) em algum momento | |
def get(self, coffee_id): | |
if coffee_id < 2: | |
store_data = self.get_coffee_info_sync(coffe_id) | |
else: | |
stored_data = yield self.get_coffee_info_async(coffe_id) # operação de IO "lenta", por isso yield | |
self.write(stored_data.title) | |
def get_coffee_info_sync(self, coffee_id): | |
result = Coffee.query.get(coffee_id) | |
return result | |
@return_future # aqui, marcando que esta função será coordenada pelo IOLoop | |
def get_coffee_info_async(self, coffee_id, callback=None): # aqui, precisa passar a funçao callback=None, o Tornado vai criar uma função de callback | |
result = Coffee.query.get(coffee_id) # operação lenta | |
callback(result) # no final, voce passa a resposta ao callback, e não return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment