简体   繁体   中英

python subprocess.Popen hangs on alternating stdout and stderr writes

I've reproduced the problem with a script that writes to x bytes to stdout and y bytes to stderr. running it using subprocess.Popen hangs.

x = 100000
y = 100000
p = subprocess.Popen(f'./a.py {x} {y}', shell=True , stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdout.read()
#Hangs here

#a.py

#!/usr/bin/python3
import sys
out=int(sys.argv[1])
err = int(sys.argv[2])

for i in range(out):
    print('a', end='')
    if i < err:
        print('a', end='', file=sys.stderr)

try to increase x and y if the problem does not reproduced.

I don't really understand the problem. But if I'm right, when I used subprocess and needed it on the same stdout, each time I used print() I've attached:

print("Your subprocess printing..", 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