Question about threading.Lock().aquire(waitflag)

A

ameyer2

It's not clear to me from the Python docs whether waitflag
is treated as a boolean or as a number.

Running on Windows, I get two different behaviors from
the following calls to acquire:

aLock = threading.Lock()
...

# Thread 0
# This one often succeeds
aLock.acquire(1)
...

# Thread 1
# When this one would fail in a different thread
aLock.acquire(2)

but the behaviors aren't what I would expect if waitflag
were a timeout value.

If the argument to acquire is a true/false flag, the
behaviors for values of 1 and 2 ought to be the same.
If it's not a true/false flag, what is it? It's not
clear to me that it's a timeout value - wait this many
seconds and then fail if the lock can't be acquired.

What I really need is a timed lock - which I can implement
myself by calling acquire(0) and sleeping and looping for
some time until it succeeds or times out. But if Python
already supports this, I'd rather do it the Python way.

Thanks.
 
T

Tim Peters

[[email protected]]
is treated as a boolean or as a number.

Running on Windows, I get two different behaviors from
the following calls to acquire:

aLock = threading.Lock()
...

# Thread 0
# This one often succeeds
aLock.acquire(1)
...

# Thread 1
# When this one would fail in a different thread
aLock.acquire(2)

but the behaviors aren't what I would expect if waitflag
were a timeout value.

It's a Windows bug, fixed a while ago but not yet in a released
Python; here's the bug report:

http://www.python.org/sf/1234979

2 _should_ have exactly the same effect as 1, but didn't on Windows.
If the argument to acquire is a true/false flag, the
behaviors for values of 1 and 2 ought to be the same.

Right, that's the intent.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top