简体   繁体   English

python 子进程。Popen 挂起交替 stdout 和 stderr 写入

[英]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.我已经使用将 x 字节写入标准输出并将 y 字节写入标准错误的脚本重现了该问题。 running it using subprocess.Popen hangs.使用subprocess.Popen运行它挂起。

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 #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.如果问题没有重现,请尝试增加 x 和 y。

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:但是,如果我是对的,当我使用 subprocess 并在同一个标准输出上需要它时,每次我使用print()我都附上了:

print("Your subprocess printing..", flush=True)

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

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