[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Fwd: Problem with threads in python????????

Gabriel Genellina

2/27/2008 1:56:00 AM

En Tue, 26 Feb 2008 01:46:48 -0200, Manikandan R <mani.agape@gmail.com>
escribió:

> Hai,
>
> Is it possible to share a single variable between multiple
> threads. For eg:
>
> Class A:
> thread_init()
>
> def run():
> fun1()
>
> def fun():
> ........ assume some arithmatic operation is going on according to the
> input given and each fun will *give different output*
> .........

Store the result inside the thread object; that is, in run, self.result =
fun1(...)

> Now 10 Thread will be running and each thread will have there individual
> output.
>
> Here is the problem now i want the sum of output of all the
> function.

From your code I imagine you have a list of Thread instances;

global_result = sum([t.result for t in list_of_threads])

--
Gabriel Genellina

1 Answer

Aaron Brady

2/27/2008 2:57:00 AM

0

On Feb 26, 7:56 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Tue, 26 Feb 2008 01:46:48 -0200, Manikandan R <mani.ag...@gmail.com>  
> escribió:
>
> > Hai,
>
> >           Is it possible to share a single variable between multiple
> > threads. For eg:
>
> > Class A:
> >          thread_init()
>
> >        def run():
> >               fun1()
>
> > def fun():
> >  ........ assume some arithmatic operation is going on according to the
> > input given and each fun will *give different output*
> > .........
>
> Store the result inside the thread object; that is, in run, self.result =  
> fun1(...)
>
> > Now 10 Thread will be running and each thread will have there individual
> > output.
>
> >        Here is the problem now i want the sum of output of all the  
> > function.
>
>  From your code I imagine you have a list of Thread instances;
>
> global_result = sum([t.result for t in list_of_threads])
>
> --
> Gabriel Genellina

You can always hash the thread id: results[get_ident()]= result.