Tornado, run coroutine method with arguments from synchronous code

According to several examples on the web, to run asynchronous methods decorated with tornado.gen.coroutine from synchronous code, you can use the following:

@tornado.gen.coroutine
def do_something():
   do_something

if __name__ == "__main__":
    tornado.ioloop.IOLoop.instance().run_sync(do_something)

      

However, if you have arguments for the coroutine method, is there a way to run it?

+3


source to share


2 answers


Yes:



@tornado.gen.coroutine
def do_something(arg):
   do_something

if __name__ == "__main__":
    tornado.ioloop.IOLoop.instance().run_sync(lambda: do_something(1))

      

+8


source


Using partial:



import functools
import tornado.gen

@tornado.gen.coroutine
def do_something(arg):
   do_something

if __name__ == "__main__":
    tornado.ioloop.IOLoop.instance().run_sync(
         functools.partial(do_something, 1))

      

+1


source







All Articles