Locking blockl to a people on a similar group / naming locks

Diez B. Roggisch deets at nospam.web.de
Wed Jan 21 04:43:30 EST 2009


Paul Rubin wrote:

> reyjexter <reyjexter at gmail.com> writes:
>> synchronize (myGroup) {
>> }
>> 
>> but how do I do this in python? how can I name the lock that will be
>> used by the thread?
> 
> You have to do it explicitly, for example with RLock:
> 
>     myInstance.lock = RLock()
>     ...
>     myInstance.lock.acquire()
>       ... critical section ...
>     myInstance.lock.release()

In python 2.5 and upwards, you can write this safer

from __future__ import with_statement # only needed for py2.5

with myInstance.lock:
   ... critical section

Diez



More information about the Python-list mailing list