threading

G

Guest

I want to get results from a thread from the main thread. In C I would use pthread_join to wait for the thread and access the result in a paramter I passed to pthread_join.

In python I can wait with join, but how would you get a result from the thread?

Thanks for any help,
Jason
 
P

Peter Hansen

I want to get results from a thread from the main thread. In C I would use pthread_join to wait for the thread and access the result in a paramter I passed to pthread_join.

In python I can wait with join, but how would you get a result from the thread?

I would always subclass the thread, as shown below, and then just grab
the result directly:

class ThreadWithResult(threading.Thread):
def __init__(self, *pargs, **kwargs)
threading.Thread.__init__(self, *pargs, **kwargs)
self.result = None

def run(self):
# do my stuff here that generates results
self.result = # the results

# and now the thread exits


In the main thread, you would:

subThread = ThreadWithResult()
subThread.start()
subThread.join()

result = subThread.result
# create a subThread.getResult() method if you don't like direct access

-Peter
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top