If your libraries are not asynchronous and do not support being run in the tornado ioloop, then the only thing you can do is run these tasks in other threads.
Essentially, there are two options, depending on whether you want to receive an return value or not:
- you put your work in a queue and some threads pull work from the queue and work these tasks off ==> no return value
- or you use the executor library (python 3.2) and send the work into the executor, add a callback to signal that the task is finished and deliver control back to tornado's ioloop (by another callback hung into the loop).
If your_task_func
is the sync-task you want offload to another thread essentially do the following:
def callback(future):
# here you want to do some stuff with the value future.result()
EXECUTOR.submit(
your_task_func
).add_done_callback(
lambda future: tornado.ioloop.IOLoop.instance().add_callback(
partial(callback, future)))
More details about this can be found in this nice write-up.
regards
markus