为什么进程没有收到队列事件?



我正在尝试使用队列将数据发送到多处理进程。由于某种原因,该示例不起作用。你知道为什么吗?

我希望程序打印出"收到一些东西:你好"和"收到毒丸",但它永远不会到达那里。但是,它确实打印了"运行"和"侦听",所以我知道它肯定会尝试从队列中读取某些内容。

我正在使用 Pythong 3.4

from multiprocessing import Process
import queue
import time
class EventAggregatorProcess(Process):
    def __init__(self):
        super(EventAggregatorProcess, self).__init__()
        self.event_queue = queue.Queue()
    def run(self):
        print('running')
        is_stopped=False
        while not is_stopped:
            print('listening')
            msg = self.event_queue.get()
            if msg:
                print('Received something: %s'%msg)
            else:
                print( 'Received poison pill' )
                is_stopped=True

if __name__=='__main__':
    eap = EventAggregatorProcess()
    eap.start()
    time.sleep(1)
    eap.event_queue.put('hello')
    eap.event_queue.put(None)

队列模块用于多线程程序。 这些线程都将位于单个进程中,这意味着它们也共享内存。 (请参阅文档末尾的"另请参阅"部分) 您的程序multiprocessing意味着您有多个进程。 这些进程不共享内存。 应使用multiprocessing库中的队列。 此版本的 Queue 处理进程间通信所需的额外工作。

from multiprocessing import Process, Queue

最新更新