为什么进程没有收到队列事件?
Why doesn't the Process receive the queue event?
我正在尝试使用队列将数据发送到多处理进程。由于某种原因,该示例不起作用。你知道为什么吗?
我希望程序打印出 "Received something: hello" 和 "Received poison pill",但它永远不会到达那里。但是,它会打印 "running" 和 "listening",所以我知道它肯定会尝试从队列中读取内容。
我正在使用 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)
queue module is for multi-threaded programs. These threads would all be in a single process, meaning they also share memory. (see the See Also section at the end of the docs) Your program is multiprocessing
meaning you have multiple processes. These processes do not share memory. You should use the Queue 来自 multiprocessing
库。此版本的 Queue
处理进程间通信所需的额外工作。
from multiprocessing import Process, Queue
我正在尝试使用队列将数据发送到多处理进程。由于某种原因,该示例不起作用。你知道为什么吗?
我希望程序打印出 "Received something: hello" 和 "Received poison pill",但它永远不会到达那里。但是,它会打印 "running" 和 "listening",所以我知道它肯定会尝试从队列中读取内容。
我正在使用 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)
queue module is for multi-threaded programs. These threads would all be in a single process, meaning they also share memory. (see the See Also section at the end of the docs) Your program is multiprocessing
meaning you have multiple processes. These processes do not share memory. You should use the Queue 来自 multiprocessing
库。此版本的 Queue
处理进程间通信所需的额外工作。
from multiprocessing import Process, Queue