[Tutor] how to return an object generated during a python threading code

Lie Ryan lie.1296 at gmail.com
Sun Dec 11 18:52:40 CET 2011


On 12/11/2011 03:46 AM, Massimo Di Stefano wrote:
> Hi All,
 >
> Trying to read the documentation, i'm looking on how to use "
> threading.Lock() " and its methods "acquire() and release()" that seems
> to be the solution to my issue
>
> ... but i'm really far to understand how to implement it in my example code.

You can call the .join() of the download threads in the main thread, so 
the main thread will wait until all download threads has finished 
downloading before inspecting lista.

...
# spawn the download threads
threads = []
for k in sitecodelist:
     get_data = download(k,lista)
     get_data.start()
     threads.append(get_data)

# wait until all download threads finishes
for th in threads:
     th.join()

# now that all download threads has finished, start processing lista
...

Alternatively, you can use a threading.Queue instead of a list for 
lista, to ensure thread safety without having to deal with (explicit) 
locking.




More information about the Tutor mailing list