Skip to content

Instantly share code, notes, and snippets.

@binux
Created November 16, 2011 12:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save binux/1369949 to your computer and use it in GitHub Desktop.
Save binux/1369949 to your computer and use it in GitHub Desktop.
call any function asynchronous in tornado
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