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 am not sure how to construct the loops for this.
In addition, I do not understand the behavior of the Queue. I would have expected that ALL squared numbers are in the queue. But instead there is only one value? (The code above has changed to store all results in "results"). Hints, comments, keywords are highly appreciated.
EDIT:
Second question:
Sorry, I thought q.get() will return all the results. But it just gives the elements in a queue like fashion.