我正在尝试实现一个由两个并行运行并通过队列进行通信的进程组成的池。
目标是让写入进程使用队列将消息传递给读取进程。
每个进程都在终端上打印反馈以便获得反馈。
这是代码:
#!/usr/bin/env python
import os
import time
import multiprocessing as mp
import Queue
def writer(queue):
pid = os.getpid()
for i in range(1,4):
msg = i
print "### writer ", pid, " -> ", msg
queue.put(msg)
time.sleep(1)
msg = 'Done'
print '### '+msg
queue.put(msg)
def reader(queue):
pid = os.getpid()
time.sleep(0.5)
while True:
print "--- reader ", pid, " -> ",
msg = queue.get()
print msg
if msg == 'Done':
break
if __name__ …
Run Code Online (Sandbox Code Playgroud)