不,其实你保证,所有的功能都催生了其中任何一个开始运行之前,因为first
不yield
产卵func
和产卵func2
之间。您可以通过测试代码进行验证自己:
from tornado import gen, ioloop
@gen.coroutine
def func():
print('func started')
yield gen.moment
print('func done')
@gen.coroutine
def func2():
print('func2 started')
yield gen.moment
print('func2 done')
@gen.coroutine
def first():
for i in range(2):
ioloop.IOLoop.current().spawn_callback(func)
ioloop.IOLoop.current().spawn_callback(func2)
yield gen.sleep(1)
ioloop.IOLoop.current().run_sync(first)
它打印:
func started
func started
func2 started
func done
func done
func2 done
见,func2
的协同程序运行func
完成之前就开始了。
来完成你想要的东西:
@gen.coroutine
def first():
yield [func() for i in range(2)]
ioloop.IOLoop.current().spawn_callback(func2)
此打印:
func started
func started
func done
func done
func2 started
func2 done
如果你想first
等待func2
完成它退出之前,则:
@gen.coroutine
def first():
yield [func() for i in range(2)]
yield func2()
有关从协程调用协程的更多信息,请参阅我的Refactoring Tornado Coroutines。
感谢您抽出时间写出杰西非常感谢。 –