Skip to main content
added 593 characters in body
Source Link
Duncan
  • 96.4k
  • 15
  • 129
  • 160

Use a queue: each thread when completed puts the result on the queue and then you just need to read the appropriate number of results and ignore the remainder:

#!python3.3 import queue # For Python 2.x use 'import Queue as queue' import threading, time, random def func(id, result_queue): print("Thread", id) time.sleep(random.random() * 5) result_queue.put((id, 'done')) def main(): q = queue.Queue() threads = [ threading.Thread(target=func, args=(i, q)) for i in range(5) ] for th in threads: th.daemon = True th.start() result1 = q.get() result2 = q.get() print("Second result: {}".format(result2)) if __name__=='__main__': main() 

Documentation for Queue.get() (with no arguments it is equivalent to Queue.get(True, None):

Queue.get([block[, timeout]])

Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

Use a queue: each thread when completed puts the result on the queue and then you just need to read the appropriate number of results and ignore the remainder:

#!python3.3 import queue # For Python 2.x use 'import Queue as queue' import threading, time, random def func(id, result_queue): print("Thread", id) time.sleep(random.random() * 5) result_queue.put((id, 'done')) def main(): q = queue.Queue() threads = [ threading.Thread(target=func, args=(i, q)) for i in range(5) ] for th in threads: th.daemon = True th.start() result1 = q.get() result2 = q.get() print("Second result: {}".format(result2)) if __name__=='__main__': main() 

Use a queue: each thread when completed puts the result on the queue and then you just need to read the appropriate number of results and ignore the remainder:

#!python3.3 import queue # For Python 2.x use 'import Queue as queue' import threading, time, random def func(id, result_queue): print("Thread", id) time.sleep(random.random() * 5) result_queue.put((id, 'done')) def main(): q = queue.Queue() threads = [ threading.Thread(target=func, args=(i, q)) for i in range(5) ] for th in threads: th.daemon = True th.start() result1 = q.get() result2 = q.get() print("Second result: {}".format(result2)) if __name__=='__main__': main() 

Documentation for Queue.get() (with no arguments it is equivalent to Queue.get(True, None):

Queue.get([block[, timeout]])

Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

Source Link
Duncan
  • 96.4k
  • 15
  • 129
  • 160

Use a queue: each thread when completed puts the result on the queue and then you just need to read the appropriate number of results and ignore the remainder:

#!python3.3 import queue # For Python 2.x use 'import Queue as queue' import threading, time, random def func(id, result_queue): print("Thread", id) time.sleep(random.random() * 5) result_queue.put((id, 'done')) def main(): q = queue.Queue() threads = [ threading.Thread(target=func, args=(i, q)) for i in range(5) ] for th in threads: th.daemon = True th.start() result1 = q.get() result2 = q.get() print("Second result: {}".format(result2)) if __name__=='__main__': main()