繁体   English   中英

bash脚本的Python subprocess.check_call不等待子流程完成

[英]Python subprocess.check_call of the bash script is not waiting for subprocesses to finish

N = 50000
with open('input', 'w') as f:
    for i in range(N):
        f.write(str(i) + '\n')

run_command = '/bin/bash -e -o pipefail -c "((sort | tee >/dev/null >(cat | (tee >/dev/null >(sort >&3)))) <input 3>output)& wait"'

subprocess.check_call(run_command, shell=True)

time.sleep(sleep_time)

print sh.wc("output", "-l")

sleep_time = 0时运行此python代码sleep_time = 0将返回0,但在sleep_time = 1返回50000。

原因似乎在于不等待bash子进程完成。 可能我对wait函数的使用不正确。 我进行了实验,但没有找到令人满意的解决方案。

当您/bin/bash您的命令将在子shell中运行,因此您将立即退出该过程。 同样,您在该子Shell中运行的命令也在后台运行,因为&末尾,您可以传递$! 作为参数,以wait它要等到最后一个后台进程退出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM