I have to use Python 2 to make asynchronous calls.
I'm using Tornado to accomplish this but if there is a better tool I can change tools.
The following code runs my sleep 2 && echo hi command asynchronously in the background
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')
The problem is that x.done() is returning True for all futures before my bash command is finished.
How can I turn process.communicate() into a future (Only done once the keyword "hi" is available) so that I can wait on all the futures to be done and get then get the output from the futures?