Skip to content Skip to sidebar Skip to footer

Python Official Docs Code: Multiprocessing "lock Example" Fails To Run

I tried to run the example code from the Official Python Docs. I am using PyCharm inside a virtualenv that it created on OSX. The code fails and prints out a very long error log. I

Solution 1:

After encountering and solving a similar multiprocessing issue in using Python on OSX here, it seems the easiest solution is to add a time.sleep(1). This allows each Process() to have enough time to get going.

The solution in code is as follows:

from multiprocessing import Process, Lock
import time

def f(l, i):
    l.acquire()
    try:
        print('hello world', i)
    finally:
        l.release()

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

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

    time.sleep(1)       # <-- Adding this solves the issue on OSX

Post a Comment for "Python Official Docs Code: Multiprocessing "lock Example" Fails To Run"