且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何写入 Python 子进程的标准输入?

更新时间:2023-11-17 23:26:52

***使用 沟通:

from subprocess import Popen, PIPE, STDOUT
p = Popen(['myapp'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_data = p.communicate(input='data_to_write')[0]

更好",因为这个警告:

"Better", because of this warning:

使用communication() 而不是.stdin.write、.stdout.read 或.stderr.read 来避免由于任何其他操作系统管道缓冲区填满并阻塞子进程而导致的死锁.

Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.