an error in python lib?

Wenhua Zhao whzhao at gmail.com
Thu Oct 11 18:06:43 EDT 2012


> On Wed, Oct 10, 2012 at 6:18 AM, Ulrich Eckhardt
>> The comment clearly states "owned by current thread", not "owned by any
>> thread". The latter would also be useless, as that can change concurrently
>> at any time when owned by a different thread, so making decisions on this
>> state is futile.

Agree.

On Wed, Oct 10, 2012 at 12:21 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> Can you demonstrate an API bug that is caused by this?

A simple demo of this error is:

-----------8<------------------------8<----------------------
import time
from threading import Condition, Lock, Thread

cv = Condition(Lock())

def do_acquire():
    cv.acquire()
    print "cv acquired in thread"
    time.sleep(5)
    cv.release()
    print "cv released in thread"

thread = Thread(target=do_acquire)
thread.start()

for i in range(10):
    print "in main cv._is_owned: ", cv._is_owned()
    time.sleep(1)
-----------8<------------------------8<----------------------

The condition variable is only acquired in the thread, so in main it should
always print false.  However, my output gives:

$ python test.py
cv acquired in threadin main cv._is_owned:
True
in main cv._is_owned:  True
in main cv._is_owned:  True
in main cv._is_owned:  True
in main cv._is_owned:  True
cv released in thread
in main cv._is_owned:  False
in main cv._is_owned:  False
in main cv._is_owned:  False
in main cv._is_owned:  False
in main cv._is_owned:  False

However, as long as you follow the generic pattern, i.e. always use
acquire() before wait(), this error is not triggered.

Thanks all.
Wenhua



More information about the Python-list mailing list