Skip to main content
 首页 » 编程设计

python之为什么管道变量不能用作python中线程间通信的管道

2025年05月04日11mengfanrong

在我探索 IPC 期间,专门处理 python 3.4.1 中的线程和套接字, 我经历了一些有点好奇的事情,我不太明白发生了什么。 目前(并且成功地(现在))我正在使用带有 os.pipe() 的匿名管道发送 持有套接字连接的线程的终止信号。

我的目标是以优雅的方式终止线程。一开始我尝试使用 bool 标志,但由于 select 调用被阻塞,我不得不向 select.select 可以读取的内容发送终止信号;套接字、管道、标准输入等,因此中断了选择调用。

在我发现如何使用管道与线程通信并穿透 select 调用之前,我将开发中断到测试分支。

让我解释一下我的情况。 基本上这有效...:

import os 
import threading 
import select 
 
class MyThread(threading.Thread): 
    def __init__(self, pipein): 
        threading.Thread.__init__(self) 
        # The pipe to listen for terminate signals on 
        self.pipein = pipein 
        self.stopped = False 
        self.inputs = [self.pipein] 
 
    def run(self): 
        print("Thread-1 Started") 
        while not self.stopped: 
            inputready, outputready, errors = select.select(self.inputs, [], []) 
            for i in inputready: 
                if i == self.pipein: 
                    signal = os.read(self.pipein, 64) 
                    # 64 An arbitrary length that should be enough in any case 
                    print("The thread received stuff on the pipe: %s" % signal) 
                    if signal == b'stop': 
                        print("Stop command received.") 
                        print("Exiting.") 
                        self.stopped = True 
                        break 
 
if __name__ == "__main__": 
 
    # Create the communication pipes 
    pipe = os.pipe() 
 
    # Start the worker thread 
    print("Starting the worker thread...") 
    t = MyThread(pipe[0]) 
    t.start() 
    print("Worker thread started.") 
 
 
    stopped = False 
 
    # Enter the main loop 
    while not stopped: 
        command = input("Command to send to thread: ") 
        os.write(pipe[1], bytes(command, 'UTF-8')) 
        stopped = True 

如果我在终端中输入“停止”,我会得到这个:

localhost:ipc.git $ python3 pipes.py 
Starting the worker thread... 
Thread-1 Started 
Worker thread started. 
Command to send to thread: stop 
The thread received stuff on the pipe: b'stop' 
Stop command received. 
Exiting. 
localhost:ipc.git $ clear 

这不是:

import os 
import threading 
import select 
 
class MyThread(threading.Thread): 
    def __init__(self, pipein): 
        threading.Thread.__init__(self) 
        # The pipe to listen for terminate signals on 
        self.pipein = pipein 
        self.stopped = False 
        self.inputs = [self.pipein] 
 
    def run(self): 
        print("Thread-1 Started") 
        while not self.stopped: 
            inputready, outputready, errors = select.select(self.inputs, [], []) 
            for i in inputready: 
                if i == self.pipein: 
                    signal = os.read(self.pipein, 64) 
                    # 64 An arbitrary length that should be enough in any case 
                    print("The thread received stuff on the pipe: %s" % signal) 
                    if signal == b'stop': 
                        print("Stop command received.") 
                        print("Exiting.") 
                        self.stopped = True 
                        break 
 
if __name__ == "__main__": 
 
    # Create the communication pipes 
    pipein, pipeout = os.pipe() # Seperate into reader fd and writer fd 
 
    # Start the worker thread 
    print("Starting the worker thread...") 
    t = MyThread(pipein) # Give the thread the receiver 
    t.start() 
    print("Worker thread started.") 
 
 
    stopped = False 
 
    # Enter the main loop 
    while not stopped: 
        command = input("Command to send to thread: ") 
        # Write on the variable of pipe[1]: pipeout 
        os.write(pipeout, bytes(command, 'UTF-8')) 
        stopped = True 

不同的是,得到一个

OSError: [Errno 9] Bad file descriptor 

当尝试从管道创建的变量中读取或写入时

喜欢:

pipein, pipeout = os.pipe() 

或

pipe = os.pipe() 
pipein = pipe[0] 
pipeout = pipe[1] 

但是,如果我使用 pipe[0] 和 pipe[1] 分别通过 os.read() 和 os.write() 进行读取和写入,它就可以正常工作!

因此,为 pipe[0] 或 pipe[1] 创建任何类型的变量都不起作用,我得到了 OSError。 如果我创建一个类调用 Communicator 并将 pipe[0] 和 pipe[1] 作为实例变量,则同样适用。

谁能解释一下为什么会这样?我永远无法写入管道 [1] 的变量,还是仅仅因为我在线程之间切换?

如果您知道可以在 select 调用中使用或中断 select 调用的另一种线程间通信方式,我会洗耳恭听。

我尝试了 io.StringIO 或 io.{OtherIOHere} 的实例,但它们不支持 fileno() 调用,因此它们无法使用 select

我想创建一个类来包含我的通信管道以获得更好的可用性,但直到我发现为什么管道变量不起作用我才做不到。

欢迎任何意见或建议。

编辑:

添加了一些调试测试:

import os 
import threading 
import time 
import select 
 
class MyThread(threading.Thread): 
    def __init__(self, pipein): 
        threading.Thread.__init__(self) 
        self.pipein = pipein 
        self.stopped = False 
        self.inputs = [self.pipein] 
 
    def run(self): 
        print("Thread-1 Started") 
        while not self.stopped: 
            inputready, outputready, errors = select.select(self.inputs, [], []) 
            for i in inputready: 
                if i == self.pipein: 
                    signal = os.read(self.pipein, 64) 
                    print("The thread received stuff on the pipe: %s" % signal) 
                    if signal == b'stop': 
                        print("Stop command received.") 
                        print("Exiting.") 
                        self.stopped = True 
                        break 
 
if __name__ == "__main__": 
 
    # Create the communication pipes 
    pipe = os.pipe() 
    pipein = pipe[0] 
    pipeout = pipe[1] 
 
    # Some Print debugs 
    print(type(pipein)) 
    print(type(pipeout)) 
    print(pipein) 
    print(pipeout) 
    print(type(pipe)) 
    print(type(pipe[0])) 
    print(type(pipe[1])) 
    print(pipe[0]) 
    print(pipe[1]) 
 
 
    # Start the worker thread 
    print("Starting the worker thread...") 
    t = MyThread(pipein) 
    t.start() 
    print("Worker thread started.") 
 
    # Enter the main loop 
 
    stopped = False 
 
 
    while not stopped: 
        command = input("Command to send to thread: ") 
        os.write(pipeout, bytes(command, 'UTF-8')) 
        stopped = True 

@ Dave,有趣的是,这现在有效,却不知道为什么。我做了同样的事情,这是两个不同的项目。在这两种情况下,我都无法写入 pipe[1] 的变量

 localhost:ipc.git $ python3 pipes.py 
<class 'int'> 
<class 'int'> 
3 
4 
<class 'tuple'> 
<class 'int'> 
<class 'int'> 
3 
4 
Starting the worker thread... 
Thread-1 Started 
Worker thread started. 
Command to send to thread: stop 
The thread received stuff on the pipe: b'stop' 
Stop command received. 
Exiting. 
localhost:ipc.git $  

编辑2

好的,我已经创建了 Communicator 类来使用管道在线程之间进行通信。它带有易于使用的 read() 和 write() 方法。一切似乎都很好。想知道为什么它以前不起作用。一定是系统相关的东西。或许我在套接字和线程方面的工作有优势。

完整的功能代码如下:

导入操作系统 导入线程 导入选择

class MyThread(threading.Thread): 
    def __init__(self, comm): 
        threading.Thread.__init__(self) 
        self.comm = comm 
        self.stopped = False 
        self.inputs = [self.comm.pipein] 
 
    def run(self): 
        print("Thread-1 Started") 
        while not self.stopped: 
            inputready, outputready, errors = select.select(self.inputs, [], []) 
            for i in inputready: 
                if i == self.comm.pipein: 
                    signal = self.comm.read() 
                    print("The thread received stuff on the pipe: %s" % signal) 
                    if signal == b'stop': 
                        print("Stop command received.") 
                        print("Exiting.") 
                        self.stopped = True 
                        break 
 
class Communicator: 
    def __init__(self): 
        self.pipe = os.pipe() 
        self.pipein = self.pipe[0] 
        self.pipeout = self.pipe[1] 
 
    def write(self, msg): 
        os.write(self.pipeout, msg) 
 
    def read(self): 
        return os.read(self.pipein, 64) 
 
if __name__ == "__main__": 
 
    # Create the communication pipes 
    #pipe = os.pipe() 
    #pipein = pipe[0] 
    #pipeout = pipe[1] 
 
    # Use the communicator class 
    comm = Communicator() 
 
 
    # Some Print debugs 
 
 
    # Start the worker thread 
    print("Starting the worker thread...") 
    t = MyThread(comm) 
    t.start() 
    print("Worker thread started.") 
 
    # Enter the main loop 
 
    stopped = False 
 
    while not stopped: 
        command = input("Command to send to thread: ") 
        comm.write(b'stop') 
        stopped = True 

感谢您的帮助。

请您参考如下方法:

我将你的两个代码示例复制并粘贴到我的 macbook 上的 2 个文件中,使用 python 3.4.1(来自 macports)运行它们,输入“停止”,它们都工作了。

您使用的是什么操作系统?

编辑:看起来您“修复”了它。好工作。 ;)