Created
March 18, 2013 20:30
-
-
Save anonymous/5190528 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
class BaseHandler(tornado.web.RequestHandler): | |
def initialize(self): | |
self.thread = None | |
def task(self, target, *args, **kwargs): | |
target_task = functools.partial(target, *args, **kwargs) | |
def callback_wrapper(callback): | |
callback(target_task()) | |
def wrapper(*args, **kwargs): | |
target_callback = (kwargs.get('callback'),) | |
self.thread = threading.Thread(target=callback_wrapper, args=target_callback) | |
self.thread.start() | |
return tornado.gen.Task(wrapper) | |
# USAGE | |
class MainHandler(BaseHandler): | |
@tornado.web.asynchronous | |
@tornado.gen.engine | |
def get(self): | |
photos = yield self.task(Photo.objects.filter, approved=True) # make a django (would be blocking) query | |
self.render('index.html', photos=photos) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment