简体   繁体   中英

Python multiprocessing: Event.wait() blocking other processes

I'm working with a toy multiprocessing problem, and event signalling is not working as expected. The multiprocessing documentation refers detail description of Event() to the multithreading documentation, and the description of the methods there are precisely what I'm trying to do. I want worker processes (subclassed from multiprocessing.Process) spawned by a parent class, then wait for a start signal from the parent class, do their thing, then terminate. What seems to be happening, however, is that the first process, once running, blocks any others. What's going on here, and how do I fix?

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)

This is only outputting "0 started", then hanging. It seems the Event.wait() is blocking all other threads, even the caller. The documentation implies this should not happen.

He is a working version of the code. When you subclass process you implement the run method to define what should run in that process. When you actually want the process to start you should call the start method on it (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)

Outputs:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM