Python多处理:Event.wait()阻塞其他进程

时间:2015-11-25 22:35:37

标签: python synchronization multiprocessing

我正在处理玩具多处理问题,并且事件信号未按预期工作。多处理文档将Event()的详细描述引用到多线程文档,并且对方法的描述正是我正在尝试做的。我想要由父类生成的工作进程(从multiprocessing.Process子类化),然后等待来自父类的启动信号,做他们的事情,然后终止。然而,似乎正在发生的事情是,第一个进程一旦运行,就会阻止任何其他进程。这里发生了什么,我该如何解决?

class Worker(Process):

    def __init__(self, my_id, caller):
        Process.__init__(self)
        self.caller = caller
        self.my_id = my_id

    def run(self):
        print("%i started"%self.my_id)
        self.caller.start_flag.wait()
        print("%i sleeping"%self.my_id)
        sleep(2000)


class ParentProcess(object):

    def __init__(self, num_procs):
        self.procs = []
        self.start_flag = Event()        
        for i in range(num_procs):
            self.procs.append(Worker(i, self))

    def run(self):
        for proc in self.procs:
            proc.run()
        self.start_flag.set()
        for proc in self.procs:
            proc.join()
            print("%i done"%proc.my_id)           


if __name__ == '__main__':
    cpus = cpu_count()    
    world = ParentProcess(cpus)
    start = time()
    world.run()
    end = time()
    runtime = end - start
    print("Runtime: %3.6f"%runtime)

这只是输出“0开始”,然后挂起。似乎Event.wait()阻止所有其他线程,甚至是调用者。文档暗示这不应该发生。

1 个答案:

答案 0 :(得分:1)

他是代码的工作版本。在子类化进程时,您可以实现run方法来定义应该在该进程中运行的内容。当你真的想要启动进程时,你应该在它上面调用start方法(proc.start())。

from multiprocessing import Process, Event
from time import time, sleep

class Worker(Process):

    def __init__(self, my_id, caller):
        Process.__init__(self)
        self.caller = caller
        self.my_id = my_id

    def run(self):
        print("%i started"%self.my_id)
        self.caller.start_flag.wait()
        print("%i sleeping"%self.my_id)
        sleep(5)


class ParentProcess(object):

    def __init__(self, num_procs):
        self.procs = []
        self.start_flag = Event()        
        for i in range(num_procs):
            self.procs.append(Worker(i, self))

    def run(self):
        for proc in self.procs:
            proc.start()
        self.start_flag.set()
        for proc in self.procs:
            proc.join()
            print("%i done"%proc.my_id)           


if __name__ == '__main__':
    cpus = 4
    world = ParentProcess(cpus)
    start = time()
    world.run()
    end = time()
    runtime = end - start
    print(runtime)

输出:

0 started
1 started
2 started
2 sleeping
0 sleeping
1 sleeping
3 started
3 sleeping
0 done
1 done
2 done
3 done
5.01037812233