You could just wrap async_function_1 and async_function_2 in another coroutine, and run it without yielding:
from tornado.ioloop import IOLoop
from tornado import gen
@gen.coroutine
def async_function_1():
yield gen.sleep(1)
print('async_function_1 done')
@gen.coroutine
def async_function_2():
yield gen.sleep(1)
print('async_function_2 done')
@gen.coroutine
def task():
yield async_function_1()
yield async_function_2()
IOLoop.current().stop() # Only for demonstration.
@gen.coroutine
def do_something_2():
print('do_something_2 done')
@gen.coroutine
def do_something():
task()
yield do_something_2()
print('do_something done')
do_something()
IOLoop.current().start()
To ensure you handle any exception thrown by task() you might do:
def handle_err(future):
try:
future.result()
except Exception as e:
# Log exception....
pass
@gen.coroutine
def do_something():
future = task()
IOLoop.current().add_future(future, handle_err)
yield do_something_2()
print('do_something done')