简体   繁体   中英

Subprocess.Popen Stdout Waiting for Program Finish

I'm running a program via subprocess.Popen and running into an unexpected issue wherein the stdout does not print live, and instead waits for the program to finish. Oddly enough, this only occurs when the program being called is written in Python.

My control program (the one using subprocess) is as follows:

import subprocess
import os
print("Warming up")
pop = subprocess.Popen("python3 signaler.py", shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
for line in pop.stdout:
    print(line)
print("I'm done")

Signaler.py:

import time
print("Running")
time.sleep(5)
print("Done running")

When I run the control program, output is as follows. The code waits 5 seconds before printing Running , despite the fact that print("Running") occurs before the actual delay.

Warming up
*waits 5 seconds*
b'Running\n'
b'123\n'
I'm done

The strange thing is that when I modify the control program to instead run a Node program, the delay functions as expected and Running is printed 5 seconds before Done Running . The Node program is as follows:

const delay = require("delay")
async function run(){
console.log("Running")
await delay(5000)
console.log("Done running")
}
run()

This issue doesn't occur when I use os.system to call signaler.py, and still occurs when I run shell=False and modify the arguments as such. Any ideas what's causing this?

It sounds like python is buffering the output of the print function and javascript isn't. You can force print statements to be flushed to stdout by calling it with the flush keyword in signaler.py

print("Running", flush=True)

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