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

Emile van Sebille emile at fenx.com
Sat Dec 10 17:58:04 CET 2011


On 12/10/2011 8:46 AM Massimo Di Stefano said...
> 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 :
>

Moving lista into the instance seems to do it...

Emile



> #####

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
         self.lista = lista
     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
         self.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!
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list