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

R

reyjexter

Hello!

Is there a way to lock a certain block to a people on similar group?
In java this is done by like this:

synchronize (myGroup) {
}

but how do I do this in python? how can I name the lock that will be
used by the thread?



-rey
 
P

Paul Rubin

reyjexter said:
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()

It's often possible to write in a style that avoids this mess.
The preferred way is usually to write isolated threads that
communicate by passing objects through queues (Queue.Queue).
This gets rid of a lot of locking hazards.
 
D

Diez B. Roggisch

Paul said:
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
 
P

Paul Rubin

Diez B. Roggisch said:
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

Good point!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top