簡體   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