global threading.Lock not locking correctly?

Tim Peters tim.one at comcast.net
Tue Feb 4 10:16:48 EST 2003


[Afanasiy]
> # My logic tells me this should never print 'OOPS', yet it does.
> # Can someone tell me why? (P.S. this is simplified example code)

But is this the actual code you ran?

> import threading, time
>
> class testthread(threading.Thread):
>
>   def run(self):
>     for x in range(40):
>
>       global lock
>       global visitors
>
>       lock.acquire

That fetches the acquire method of the lock, and ignores it.  If you want to
lock the beast, you need

        lock.acquire()

instead.

>       visitors += 1
>       if visitors > 1:
>         print 'OOPS!'+str(visitors),
>       else:
>         print '.',
>       visitors -= 1
>
>       lock.release

Likewise, this doesn't release the lock unless you call it.






More information about the Python-list mailing list