Leo Jay wrote: > 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'?
threads are subprograms, not functions. to allow a thread to generate values, create a shared Queue object and let your threads add stuff to that queue. an example: import threading import Queue import time, random class Worker(threading.Thread): def __init__(self, index, queue): self.__index = index self.__queue = queue threading.Thread.__init__(self) def run(self): # pretend we're doing something that takes 10-100 ms time.sleep(random.randint(10, 100) / 1000.0) # pretend we came up with some kind of value self.__queue.put((self.__index, random.randint(0, 1000))) queue = Queue.Queue() for i in range(10): Worker(i, queue).start() # start a worker for i in range(10): print "worker %d returned %d" % queue.get() </F> -- http://mail.python.org/mailman/listinfo/python-list