Here is my code:
from multiprocessing import Process, RLock
import os
LOCK = RLock()
def acquire_lock():
r = LOCK.acquire()
print(os.getpid(), "lock acquired:", r)
if __name__ == "__main__":
acquire_lock()
process1 = Process(target=acquire_lock)
process1.start()
process2 = Process(target=acquire_lock)
process2.start()
process1.join()
process2.join()
With the above code, I’d expect one line printed to the stdout and the execution should not exit; but I get three lines, and the execution exited:
C:workspacenapnap_testvalidator>appsPython39python.exe .rlock.py
36660 lock acquired: True
20624 lock acquired: True
36792 lock acquired: True
C:workspacenapnap_testvalidator>
Same result swicthing from RLock to Lock.
Is the use of multiprocessing.RLock in the above code snippet correct?
2