import sys import queue import threading from lib import worker num_worker_threads = 4 def input(): return sys.stdin q = queue.Queue() threads = [] for i in range(num_worker_threads): thread_name = 'bob-' + str(i) t = threading.Thread(target=worker.worker, args=(q,), name=thread_name) t.start() threads.append(t) for item in input(): q.put(item) # block until all tasks are done q.join() # stop workers for i in range(num_worker_threads): q.put(None) for t in threads: t.join()