Tornado 龙卷风执行命令生成回调
如果我有以下类似的内容:Tornado 龙卷风执行命令生成回调,tornado,Tornado,如果我有以下类似的内容: @tornado.gen.coroutine def first(x): # # do stuff for i in I: tornado.ioloop.IOLoop.current().spawn_callback(func,i) tornado.ioloop.IOLoop.current().spawn_callback(func2,z) yield first(xxx) 我可以保证for循环中的所有派
@tornado.gen.coroutine
def first(x):
#
# do stuff
for i in I:
tornado.ioloop.IOLoop.current().spawn_callback(func,i)
tornado.ioloop.IOLoop.current().spawn_callback(func2,z)
yield first(xxx)
我可以保证
for
循环中的所有派生函数都将在最后一次派生对func2()的回调之前运行吗 否,事实上,您可以保证在任何函数开始运行之前生成所有函数,因为在生成func
和生成func2
之间,first
不会产生。您可以通过测试代码来验证这一点:
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
如果希望先
等待func2
完成后退出,则:
@gen.coroutine
def first():
yield [func() for i in range(2)]
yield func2()
有关从协同程序调用协同程序的更多信息,请参见my。否,事实上,您可以保证在任何函数开始运行之前生成所有函数,因为首先不会在生成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
如果希望先
等待func2
完成后退出,则:
@gen.coroutine
def first():
yield [func() for i in range(2)]
yield func2()
有关从coroutines致电coroutines的更多信息,请参阅我的。感谢您抽出时间撰写Jesse非常感谢的文章。感谢您抽出时间撰写Jesse非常感谢的文章。