Alexei Ustyuzhaninov writes:
But generally RLock is inappropriate for this purpose in python. Take a look at this test for example:
09:50:33 aleks@aiu:~/testRLock $ cat test.py import os import threading import time
lock=threading.RLock() if os.fork()==0: # Child process time.sleep(1) lock.acquire() print "Process",os.getpid(),": the child acquired the lock" else: # Parent process lock.acquire() print "Process",os.getpid(),": the parent acquired the lock" time.sleep(5) lock.release() print "Process",os.getpid(),"released the lock"
09:52:46 aleks@aiu:~/testRLock $ python test.py Process 6164 : the parent acquired the lock Process 6165 : the child acquired the lock Process 6165 released the lock Process 6164 released the lock Please retry this with threads!
While Linux uses processes to implement threads, these processes are more tightly coupled than those created by "fork". A fork'ed process has its own address space and its own independent resources (such as locks). Threads, on the other hand, share the same address space and the same resources. Locks will synchronize threads but not (independent) processes. Dieter