returning a value from a thread

David Bolen db3l at fitlinxx.com
Wed Jul 14 17:22:45 EDT 2004


Ken Godee <ken at perfect-image.com> writes:

(...)
> 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.
(...)
> 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.

Have you considered simply making the function in module2 return the
thread object that it has started as its response?  That thread object
could easily support an interface to interrogate its status or result
(when available).

That's not very different from returning a Queue object to which the
thread will write its result, but by returning the thread object
directly you can support a much richer interface for the original
caller, whether a blocking call to wait for a result, or a way to
interrogate if a result is available yet.  It would also work with
more than a single result or action to perform upon completion if you
needed that.

-- David



More information about the Python-list mailing list