Thread Question

Bryan Olson fakeaddress at nowhere.org
Sat Aug 5 04:01:21 EDT 2006


Carl Banks wrote:
> Ritesh Raj Sarraf wrote:
>> Carl Banks wrote:
>>> Then change the zipping part of download_from_web to acquire and
>>> release this lock; do zipfile operations only between them.
>>>
>>> ziplock.acquire()
>>> try:
>>>     do_all_zipfile_stuff_here()
>>> finally:
>>>     ziplock.release()
>> 
>> I hope while one thread has acquired the lock, the other threads (which
>> have done the downloading work and are ready to zip) would wait.
> 
> Exactly.  Only one thread can hold a lock at a time. 

In the code above, a form called a "critical section", we might 
think of a thread as holding the lock when it is between the
acquire() and release(). But that's not really how Python's 
locks work. A lock, even in the locked state, is not held by
any particular thread.

> If a thread tries
> to acquire a lock that some other thread has, it'll wait until the
> other thread releases it. 

More accurate: If a thread tries to acquire a lock that is in
the locked state, it will wait until some thread releases it.
(Unless it set the blocking flag false.) If more that one thread
is waiting to acquire the lock, it may be blocked longer.

I think the doc for threading.Lock is good:

     http://docs.python.org/lib/lock-objects.html


-- 
--Bryan



More information about the Python-list mailing list