Suppose there is a code explaining the work of threads and queues on the Internet, but the list does not return the elements in order:
import queue from threading import Thread def do_work(item): print("processing", item) def source(): for item in range(1, 10000): print("starting", item) yield item def worker(): global list_t while True: item = q.get() do_work(item) list_t.append(item) q.task_done() q = queue.Queue(maxsize=0) def main(): for i in range(10): t = Thread(target=worker) t.daemon = True t.start() for item in source(): q.put(item) q.join() list_t = [] main() print(list_t) I just can’t understand the essence of the queues, in my understanding it looks like this: 1. Serving queues are created 2. I take some data and stuff it into a queue 3. The data will appear in the flow I add to the list in this example) 5. I inform you that the work with this data 6 has been completed. Further, I analyze these data in turn. The threads disassemble the data from the queues and try to add them to the list (almost at the same time). , until task_done () notifies the other These threads cannot add to the list_t list, isn’t this the point of queues? With large amounts of data and multithreading, the threads disassemble the queue instantly, but the processing time is different, in general I do not understand the principle of the queues? explain on fingers