At first, for some reason, I wanted to say that the distribution is uneven - that is, some of the flows will be idle, and the one that was created first of all will be puffed up for everyone. Apparently such a hasty conclusion due to the naive representation of the flows in the imagination. But experiment (py3) - the tasks appear two times slower than they are processed:
import queue import threading import time import random counter = {} lock = threading.Lock() def worker(): while True: try: sleep = q.get() if sleep == "DIE": q.task_done() return print(threading.current_thread().name) with lock: counter[threading.current_thread().name] = counter.get(threading.current_thread().name, 0) + 1 time.sleep(sleep) q.task_done() except queue.Empty: pass q = queue.Queue() pool = [threading.Thread(target=worker, name="Worker " + str(i)) for i in range(4)] [t.start() for t in pool] for i in range(100): rand_sleep = random.random() / 8 q.put(rand_sleep) time.sleep(rand_sleep * 2) for i in range(4): q.put("DIE") q.join() print("Report: ", counter)
Here is what the script displays:
output: ('Report: ', {'Worker 2': 25, 'Worker 3': 25, 'Worker 0': 25, 'Worker 1': 25})
As can be seen from the results - for my code the distribution is uniform.
If tasks arrive without delays, then the distribution is uneven and depends on how long each task is processing the task.
Addition: this script was tested in Windows and Ubuntu, Python3.4 and Python2.7 were used. In all four cases, the results are the same.
threading.pooldoes not exist. Perhaps you meantmultiprocessing.pool.ThreadPool. 2- You can call various functions using ThreadPool — there is no need to reinvent it. 3- In any case: what difference does the flow of work do? If you are trying to improve some characteristic by “uniformity” of distribution, then ask directly about this characteristic. - jfs