Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2 views
Skip to first unread message

reyjexter

unread,
Jan 21, 2009, 4:04:53 AM1/21/09
to
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

Paul Rubin

unread,
Jan 21, 2009, 4:09:48 AM1/21/09
to
reyjexter <reyj...@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()

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.

Diez B. Roggisch

unread,
Jan 21, 2009, 4:43:30 AM1/21/09
to
Paul Rubin wrote:

> reyjexter <reyj...@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

Paul Rubin

unread,
Jan 21, 2009, 5:06:52 AM1/21/09
to
"Diez B. Roggisch" <de...@nospam.web.de> writes:
> 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!

0 new messages