There is a built-in IOLoop method in IOLoop to start a single call and then stop the loop, so it's pretty simple to add an event loop to a simple python script if you have a tornado in PYTHONPATH.
With a concrete example:
from tornado import gen, ioloop @gen.coroutine def another_async_func(x): print "aaf" raise gen.Return(x + 1) @gen.coroutine def myfunc(x): print "myfunc" y = yield another_async_func(x) print "back" raise gen.Return(y) @gen.coroutine def main(): y = yield myfunc(1) print "Callback called with %d" % y if __name__ == "__main__": ioloop.IOLoop.instance().run_sync(main)
It is output:
myfunc aaf back Callback called with 2
Note that run_sync poorly nested; if you call run_sync in a function called run_sync in the same IOLoop , then ending the internal call stops IOLoop and no further yield after returning the internal call.
source share