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

Massimo Di Stefano massimodisasha at gmail.com
Sat Dec 10 17:46:33 CET 2011


Hi All,

i'm tring to learn how to use threads in python to save a list of object. i'm starting from this code :

#####
import threading
import urllib
from tempfile import NamedTemporaryFile

singlelock = threading.Lock() 

class download(threading.Thread):
    def __init__(self, sitecode, lista):
        threading.Thread.__init__(self)
        self.sitecode = sitecode
        self.status = -1

    def run(self):
        url = "http://waterdata.usgs.gov/nwis/monthly?referred_module=sw&site_no="
        url += self.sitecode 
        url += "&PARAmeter_cd=00060&partial_periods=on&format=rdb&submitted_form=parameter_selection_list"
        tmp = NamedTemporaryFile(delete=False)
        urllib.urlretrieve(url, tmp.name)
        print "loaded Monthly data for sitecode : ",  self.sitecode 
        lista.append(tmp.name)
        print lista

sitecodelist = ["01046500", "01018500", "01010500", "01034500", "01059000", "01066000", "01100000"]
lista = []


for k in sitecodelist:
    get_data = download(k,lista)
    get_data.start()

#####

it just print out the list generated during the thread execution, while i'm tring to return it. 

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.

thanks so much for any hints!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111210/f0a9b637/attachment-0001.html>


More information about the Tutor mailing list