2

コードから始めます。十分に単純であることを願っています。

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

キューを適切に共有していないか、他のエラーを起こしているようです。理論的にはキューに2つの要素がある場合、プログラムはその「self.commands_q.get()」で永遠にスタックします

4

1 に答える 1

5

Queue オブジェクトをプロセス間で共有するには、Queue.Queue の代わりに multiprocessing.Queue を使用する必要があります。

こちらをご覧ください:マルチプロセッシング キュー

于 2013-05-15T17:10:52.473 に答える