[Python] python threads, how to store results from a multithread code

Daniele Maccio komradstudios a gmail.com
Ven 9 Dic 2011 21:35:51 CET


Premetto che non ho avuto il tempo per leggere approfonditamente il
tuo codice, ma non potresti semplicemente fare lista.append(path)
dentro il tuo thread, avendo la premura di mettere il codice sotto
lock?

Il 09 dicembre 2011 21:18, Massimo Di Stefano
<massimodisasha a gmail.com> ha scritto:
> Ciao,
>
> sto provando ad imparare ad usare la libreria thread … ma ho dei problemi :-(
>
> il mio obiettivo e'  il download di alcuni file da internet con urlib
> salvarli in un file temporaneo
> e scrivere in una lista il path di tali file.
>
> is codice di seguito, e'  scarica i file e li salva come file temporanei
> … ma non riesco a capire come salvarne una lista dei path.
>
> ########################################################################################
> # thread test
>
> import threading
> import urllib
> from tempfile import NamedTemporaryFile
>
> class download(threading.Thread):
>    def __init__(self, sitecode):
>        threading.Thread.__init__(self)
>        self.sitecode = sitecode
>        self.status = -1
>
>    def run(self):
>        #global filelist
>        #filelist = []
>        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
>        #filelist.append(tmp.name)
>        #return filelist
>
>
>
> sitecodelist = ['01046500', '01018500', '01010500', '01034500', '01059000', '01066000', '01100000']
>
> for k in sitecodelist:
>    get_data = download(k)
>    get_data.start()
>
> ########################################################################################
>
> per gevent il seguente codice funziona e mi restituisce :
>
>
> ########################################################################################
> # gevent test
>
> import gevent
> from gevent import monkey
> import urllib
> from tempfile import NamedTemporaryFile
>
>
> def gen_data(sitecode, listapath):
>    url1 = 'http://waterdata.usgs.gov/nwis/monthly?referred_module=sw&site_no='
>    url1 += sitecode
>    url1 += '&PARAmeter_cd=00060&partial_periods=on&format=rdb&submitted_form=parameter_selection_list'
>    tmp1 = NamedTemporaryFile(delete=False)
>    urllib.urlretrieve(url1, tmp1.name)
>    listapath.append(tmp1.name)
>
>
>
> sitecodelist = ['01046500', '01018500', '01010500', '01034500', '01059000', '01066000', '01100000']
> lista = []
>
> monkey.patch_all()
> jobs = [gevent.spawn(gen_data, sitecode, lista) for sitecode in sitecodelist]
> gevent.joinall(jobs)
>
> print lista
> ############################################################################################
>
>
> ho provato a passare un lista vuota come input e farne l'append durante l'esecuzione dei trheads .. ma sono lontano anni luce dal farlo funzionare.
>
> ho provato ad usare la libreria gevent e con essa riesco a salvare la suddetta lista … ma mi introduce ulteriori dipendenze
> (su linux nn sono un problema, ma su osx richiede di installare la libreria event da sorgente (non ho trovato binary), per poi installare gevent usando easy_install)
>
>
> potete aiutarmi a comprendere come utilizzare la libreria thread in modo da restituirmi dei valori da salvare in un oggetto python (oin questo caso una lista di path) ?
>
> grazie mille per qualsiasi aiuto!
>
> Massimo.
>
> _______________________________________________
> Python mailing list
> Python a lists.python.it
> http://lists.python.it/mailman/listinfo/python


Maggiori informazioni sulla lista Python