Join a thread and get the return value of a function

mattia gervaz at gmail.com
Fri Dec 25 06:18:06 EST 2009


Il Fri, 25 Dec 2009 05:19:46 +1100, Lie Ryan ha scritto:

> import threading
> 
> class MyThread(threading.Thread):
>      def join(self):
>          super(MyThread, self).join()
>          return self.result
> 
> class Worker(MyThread):
>      def run(self):
>          total = 0
>          for i in range(random.randrange(10000, 100000)):
>              total += i
>          self.result = total
> 
> import random
> ts = [Worker() for i in range(100)]
> for t in ts:
>      t.start()
> 
> for t in ts:
>      print t.join()

Thank you. And merry christmas!



More information about the Python-list mailing list