[Zope] How to prevent concurrent access to the same object?

Alexei Ustyuzhaninov aiu@quorus-ms.ru
Tue, 12 Feb 2002 10:37:31 +0500


Dieter Maurer wrote:

> Alexei Ustyuzhaninov writes:
>  > > Alexei Ustyuzhaninov writes:
>  > >  > Is it possible to make a zope object which doesn't allow simultaneous 
>  > >  > access to itself?
>  > > You may have a look at my product "SharableResource":
>  > > 
>  > >   <http://www.dieter.handshake.de/pyprojects/zope>
>  > 
>  > 
>  > Thanks. Unfortunately RLock-based methods don't work in this situation 
>  > (in linux at least) because concurrent transactions are executed in 
>  > separate processes, not in threads of a single process.
> I do not believe you. If you were right, then the Linux thread
> implementation were severely broken.
> 
>    Locks are intended to coordinate concurrent threads.
>    There is no other purpose for them. I am sure they work
>    even though Linux implements threads by processes.


Well, of course I had to try your product before comment, sorry. 
Probably zope kernel is designed in such way that it can use RLock to 
synchronize its child processes.

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

-- 
Alexei