Thread locking question.

MRAB google at
Sat May 9 17:57:11 CEST 2009

Piet van Oostrum wrote:
>>>>>> grocery_stocker <cdalten at> (gs) wrote:
>> gs> The following code gets data from 5 different websites at the "same
>> gs> time".
>> gs> start = time.time()
>> gs> def main():
>> gs>     for i in range(5):
>> gs>         t = MyUrl(queue)
>> gs>         t.setDaemon(True)
>> gs>         t.start()
>> gs>     for host in hosts:
>> gs>         print "pushing", host
>> gs>         queue.put(host)
>> gs>     for i in range(5):
>> gs>         queue.put(None)
>> gs>     t.join()
>> gs> if __name__ == "__main__":
>> gs>     main()
>> gs>     print "Elapsed Time: %s" % (time.time() - start)
>> gs> How does the parallel download work if each thread has a lock? When
>> gs> the program opens, it places a lock on the thread,
>> gs> right? If so, then doesn't that mean the other 4 sites have to wait
>> gs> for the thread to release the lock?
> No. Where does it set a lock? There is only a short lock period in the queue
> when an item is put in the queue or got from the queue. And of course we
> have the GIL, but this is released as soon as a long during operation is
> started - in this case when the Internet communication is done.

Also, the code is creating 5 threads, but using join() on only the last

More information about the Python-list mailing list