Created
November 16, 2011 12:21
-
-
Save binux/1369949 to your computer and use it in GitHub Desktop.
call any function asynchronous in tornado
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 AsyncProcessMixin(object): | |
def call_subprocess(self, func, callback=None, args=[], kwargs={}): | |
self.ioloop = tornado.ioloop.IOLoop.instance() | |
self.pipe, child_conn = Pipe() | |
def wrap(func, pipe, args, kwargs): | |
try: | |
pipe.send(func(*args, **kwargs)) | |
except Exception, e: | |
logging.error(traceback.format_exc()) | |
pipe.send(e) | |
self.ioloop.add_handler(self.pipe.fileno(), | |
self.async_callback(self.on_pipe_result, callback), | |
self.ioloop.READ) | |
thread.start_new_thread(wrap, (func, child_conn, args, kwargs)) | |
def on_pipe_result(self, callback, fd, result): | |
try: | |
ret = self.pipe.recv() | |
if isinstance(ret, Exception): | |
raise ret | |
if callback: | |
callback(ret) | |
finally: | |
self.ioloop.remove_handler(fd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment