returning a value from a thread

Ken Godee ken at perfect-image.com
Wed Jul 14 16:04:26 EDT 2004


>>ie. store value in memory from module3 and be able to
>>read it from module1.
>>
>>The closest I can think of is to pass a queue reference
>>and stor values from module3 in the queue and then read them
>>back while in module1.
> 
> 
> Actually, any kind of container object would do: you could pass a list, 
> dictionary, or class to the thread, and the thread could store its results 
> in it.

This was my orginal problem, being the middle man (module2) that spins
the thread(calling function in module3) has returned to module1 before 
the thread result was available. So I couldn't figure a way to get the 
result from module3 back to a global var in module1, since middle man 
was gone.

> 
> A hackish method would be to pass the dictionary returned by locals() in 
> module1 to the thread. This way the thread could set a value in module1 
> directly.
> 
Hhmmm, interesting, didn't know you could do that. Might have to try it.

> Just don't forget to .join() on the thread in module1 before accessing the 
> container object! (Assuming you're using threading) I just wish Python's 
> .join() could return values, like pthread_join can.
> 
Using thread here.
But I really have nothing against using queue, I've used queues before 
but more for a kind of stream of data and was thinking I was missing
something because I only want to pass back a single value, so Queue(1) 
would probally be alright.

Thanks for all the responses.

Ken






More information about the Python-list mailing list