Need something like RR.

Will the distribution of tasks in the Queue.Queue() queue be uniform across threads? It is embarrassing that tasks will appear more slowly than the execution of commands on them.

I run several threads (with different code for different devices) and a thread that accepts tasks via http. The task enters the queue, and before that the workers hang a blocked queue.get ().

I used in another project multiprocessing.dummy.Pool - everything is nice and even there, but now the threads have different code inside and you cannot do with one function.

The task is to unload the executive devices, not the processor.

  • 1- threading.pool does not exist. Perhaps you meant multiprocessing.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
  • 1-yes, I’m more about multiprocessing.dummy.Pool. 2- the task to unload the actuators - eri

1 answer 1

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.

  • What does "uniformity" here give? Suppose all the work would be performed only one thread here — what negative consequences do you see? Considering that the input is slower than processed — it is not clear why there should be more than one stream at all. - jfs
  • and if the "work" itself is not performed by a computer? An example is to distribute applications between people (so that their salary is uniform). another example of sending text messages from several modems (300 sms each in the subscription fee) - eri
  • @jfs, uniformity gives nothing. If it is known that the tasks arrive slower than they are processed (for example, once a second from a sensor and the result must be placed in the database), then you can do without additional threads by simplifying the code. With one stream, negative consequences - you can tighten the loss of scalability - tomorrow you will connect one hundred new sensors and without async one stream will not have time to process everything - while it waits for a response from the database, new information will come and so accumulate while there is enough memory. - m9_psy
  • @eri, if the work is not done by a computer, then it’s not at all clear what the Python threads are about. To simulate a conditional 'distribution of applications in bookkeeping' there is a Queuing theory with which you can very easily and quickly simulate any such situation. There are even special programming languages ​​(GPSS) for this. True, I have never seen anyone seriously engaged in this. - m9_psy
  • and who will distribute the tasks?) - eri