python threads - always have x active threads when iterating over n tasks
问题 What I basically want to do is the following: import threading import Queue def test_thread(elem, q): q.put(elem ** 2) a = [1,2,3,4,5,6,7,8] q = Queue.Queue() results = [] for x in range(8): print x threading.Thread(target=test_thread, args=(a[x], q)).start() results.append(q.get()) But instead of running all threads at once I want to run only say 2 in parallel adn iterate over the list. Once one thread is done the next value from the list should be processed. I could't find an example and I