我必须使用python 2进行异步调用。
我用龙卷风来完成这个任务,但是如果有更好的工具,我可以换工具。
下面的代码运行
sleep 2 && echo hi
后台异步命令
from tornado import gen
import subprocess
import time
@gen.coroutine
def longProcess():
bashCommand = "sleep 5 && echo hi"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
yield output
futures = [longProcess() for x in range(0, 5)]
while True:
if all(x.done() == True for x in futures):
break
time.sleep(1)
print('All futures resolved')
问题是
x.done()
正在回归
True
在我的bash命令完成之前的所有未来。
我怎样才能转身
process.communicate()
进入未来(只做一次关键字“嗨”),这样我就可以等待所有的期货交易完成,然后得到期货的输出。