Skip to main content
 首页 » 编程设计

python之使用python的子进程模块打开一个python进程

2025年05月04日11lexus

我正在尝试在 python 脚本中与不同的 python 解释器通信。我写了一个对象,它应该存储子进程并读/写它的标准输入、标准输出、标准错误。

import subprocess 
import fcntl 
import os 
 
class Python: 
 
    def __init__(self): 
        self.process = subprocess.Popen("python", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
        fcntl.fcntl(self.process.stdout, fcntl.F_SETFL, os.O_RDONLY | os.O_NONBLOCK) 
        fcntl.fcntl(self.process.stderr, fcntl.F_SETFL, os.O_RDONLY | os.O_NONBLOCK) 
 
    def read(self): 
        stdout = self.process.stdout.read() 
        if stdout: 
            print("stdout:", stdout) 
        stderr = self.process.stderr.read() 
        if stderr: 
            print("stderr:", stderr) 
 
    def exec(self, line): 
        self.process.stdin.write(bytes((line + "\n").encode("ascii"))) 
        self.process.stdin.flush() 

在 init 函数中,子进程被创建,stdout、stderr 被设置为非阻塞模式。 read 函数只是暂时将 stdout、stderr 打印到屏幕上,exec 函数向 python 的 stdin 写入一行并将其刷新。我使用一个简单的回显脚本对此进行了测试:

while True: 
    print(input()) 

我能够使用我的 exec 方法,稍后使用 read 方法读取我传递给 exec 方法的行。

我的问题是这不适用于 python 解释器。我试图将所有内容写入其标准输入,但它没有将任何内容写入标准输出、标准错误。

请您参考如下方法:

好吧,我做了一些测试,您的代码可以正常工作。这意味着您正确地创建了一个 Python 解释器并将命令传递给它,并且解释器正确地执行了它们。

问题是管道的输出可能会被缓冲(并且在这里)。如果您正在写入几千字节,您可以获得开始部分,但在这里,在 Python 解释器退出之前,实际上没有任何内容写入管道。

确认方式:

p = Python() 
p.exec("print('foo')") 
p.exec("print('bar')") 
p.process.stdin.close() # force interpreter to exit ... 
time.sleep(0.1) # wait for the interpreter to exit ... 
p.read() 

然后你应该得到 stdout: b'foo\r\nbar\r\n'


正如 J.F.Sebastian 在其评论中所建议的,不被缓冲打扰的最直接的方法是简单地要求解释器不要缓冲任何东西,使用 -u 选项或使用 PYTHONUNBUFFERED 环境变量:

class Python: 
 
    def __init__(self): 
        self.process = subprocess.Popen("python -u", stdin=subprocess.PIPE, 
                                         stdout=subprocess.PIPE, 
                                         stderr=subprocess.PIPE) 
        ...