threading

Peter Hansen peter at engcorp.com
Tue Jun 29 15:47:35 EDT 2004


jesso1607 at rogers.com wrote:

> 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



More information about the Python-list mailing list