我有一个Python助手函数可以并行运行grunt命令,使用Popen
来处理子进程,其目的是通过CLI实现通信。问题在于当需要所有这些进程的任何用户输入时,例如文件路径、密码、“是/否”决策:
Enter file path: Enter file path: Enter file path: Enter file path: Enter file path: Enter file path: Enter file path:
Everything up-to-date
Grunt task completed successfully.
用户只需要提供一次输入,一个进程成功完成,所有其他进程都不会继续执行。
代码:
from subprocess import check_output, Popen
def run_grunt_parallel(grunt_commands):
return_code = 0
commands = []
for command in grunt_commands:
with tempfile.NamedTemporaryFile(delete=False) as f:
app = get_grunt_application_name(' '.join(command))
commands.append({'app': app, 'process': Popen(command, stdout=f)})
while len(commands):
sleep(5)
next_round = []
for command in commands:
rc = command['process'].poll()
if rc == None:
next_round.append(command)
else:
if rc == 0:
else:
return_code = rc
commands = next_round
return return_code
有没有办法确保用户为每个流程提供所有必要的输入?