how to get the return value of a thread?

Benjamin Niemann pink at odahoda.de
Fri Sep 9 22:41:45 CEST 2005


Leo Jay wrote:

> Dear all,
> 
> i would like to get the return value of all threads
> 
> e.g.
> def foo(num):
>     if num>10:
>         return 1
>     elif num>50:
>         return 2
>     else
>         return 0
> 
> 
> after i invoked
> t = thread.start_new_thread(foo,(12,))
> how to get the return value of `foo'?

Take a look at the Queue module. Create a queue instance at let the 'foo
thread' put() its result into it:

fooResult = Queue.Queue()

def foo(num):
    result = 0

    if num>10:
        result = 1
    elif num>50:
        result = 2

    fooResult.put(result)

t = thread.start_new_thread(foo,(12,))

# do other stuff, foo is running in background

r = fooResult.get() # guaranteed to block until result is available
print r

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list