Начну с кодом, я надеюсь, что это достаточно просто:Как «слушать» в очередь многопроцессорной в Python
import Queue
import multiprocessing
class RobotProxy(multiprocessing.Process):
def __init__(self, commands_q):
multiprocessing.Process.__init__(self)
self.commands_q = commands_q
def run(self):
self.listen()
print "robot started"
def listen(self):
print "listening"
while True:
print "size", self.commands_q.qsize()
command = self.commands_q.get()
print command
if command is "start_experiment":
self.start_experiment()
elif command is "end_experiment":
self.terminate_experiment()
break
else: raise Exception("Communication command not recognized")
print "listen finished"
def start_experiment(self):
#self.vision = ds.DropletSegmentation()
print "start experiment"
def terminate_experiment(self):
print "terminate experiment"
if __name__ == "__main__":
command_q = Queue.Queue()
robot_proxy = RobotProxy(command_q)
robot_proxy.start()
#robot_proxy.listen()
print "after start"
print command_q.qsize()
command_q.put("start_experiment")
command_q.put("end_experiment")
print command_q.qsize()
raise SystemExit
Поэтому в основном я запускаю процесс, и я хочу, чтобы этот процесс слушать команды, помещенные в очередь.
Когда я выполняю этот код, я получаю следующее:
after start
0
2
listening
size 0
, кажется, что я не разделяя очереди должным образом, или что я делаю любую другую ошибку. Программа застряла навсегда в этом «self.commands_q.get(), когда теоретически очередь имеет 2 элемента.