简体   繁体   中英

Python Synchronization Multiprocessing Lock

I've gone through (this SO thread)[ Synchronization issue using Python's multiprocessing module but it doesnt provide the answer.

The following code :-

rom multiprocessing import Process, Lock

    def f(l, i):
        l.acquire()
        print 'hello world', i
        l.release()
        # do something else

    if __name__ == '__main__':
        lock = Lock()

        for num in range(10):
            Process(target=f, args=(lock, num)).start()

How do I get the processes to execute in order.? I want to hold up a lock for a few seconds and then release it and thereby moving forward with the P1 and P2 into the lock, and then P2 moving forward and P3 exceuting that lock. How would I get the processes to execute in order.?

It sounds like you just want to delay the start of each successive process. If that's the case, you can use a multiprocessing.Event to delay starting the next child in the main process. Just pass the event to the child, and have the child set the Event when its done doing whatever should run prior to starting the next child. The main process can wait on that Event , and once it's signalled, clear it and start the next child.

from multiprocessing import Process, Event

def f(e, i):
    print 'hello world', i
    e.set()
    # do something else

if __name__ == '__main__':
    event = Event()

    for num in range(10):
        p = Process(target=f, args=(event, num))
        p.start()
        event.wait()
        event.clear()

this is not the purpose of locks. Your code architecture is bad for your use case. I think you should refactor your code to this:

from multiprocessing import Process    

def f(i):
    # do something here

if __name__ == '__main__':
    for num in range(10):
        print 'hello world', num
        Process(target=f, args=(num,)).start()

in this case it will print in order and then will do the remaining part asynchronously

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