how to get the return value of a thread?

L

Leo Jay

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'?

Thanks
 
B

Benjamin Niemann

Leo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
48,001
Latest member
Wesley9486

Latest Threads

Top