简体   繁体   中英

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")

Running this python piece of code with sleep_time = 0 returns 0, but with sleep_time = 1 returns 50000.

The reason seems to be in not waiting for bash subprocesses to finish. Probably, my usage of the wait function is not correct. I made experiments, but no satisfactory solution found.

When you /bin/bash your command is going to run in a subshell, so you are immediately exiting the process. Also the command you are running in that subshell is running in the background, because of the & at the end, you can pass $! as parameters to wait for it to wait until the last background process exits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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