directory

  • Python thread queue classification
  • Python thread first in, first out Queue
  • Python threadfirst in, first out Queue (fifO
  • The Python thread fifO Queue is used
  • Five. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

The previous article explains a lot of knowledge about communication between threads, such as: thread mutex LOCK, thread event, thread condition variable, etc., these are often used in the development of content, but today continues to explain a more important knowledge point – thread queue;

Python thread queue classification

  • 1. Thread Queue – FIFO(first in, first out)****, i.e. which data is stored first, which data is fetched first when the data is fetched, the same as the Queue to buy things in life
  • 2. Thread queue LifoQueue — LIFO(first in, last out queue)****, that is, which data is stored last, the data is taken first, the same as the pistol magazine in life, the last bullet into the first out
  • 3. PriorityQueue – PriorityQueue ****, that is, a priority is added when data is stored, and the one with the highest priority is taken out when data is fetched

Today, I’ll only cover the first common thread Queue(FIFO), and I’ll cover the next two in more detail!

Python thread first in, first out Queue

Thread queues, also known as FIFOS, contain data in a first-in, first-out (FIFO) Queue, which is like having diarrhea.

Take a visual example: if we put the six numbers, 123456, in sequence in the queue, then we requeue the data, the first data must be 1, the second data must be 2, and so on, this is the so-called eat what pull what – FIFO (first in, first out);

Python threadfirst in, first out Queue (fifO

  • Qsize – ** Returns the size of the Queue
  • Empty – ** Determines whether the Queue is empty
  • **Queue. Full – ** Determines whether the Queue is full
  • Queue.get([block[,timeout]]) – Removes an item from the Queue head and returns it. Block defaults to True, meaning that when the Queue is empty it will block until an item is available. False means that an exception will be raised when you get when the queue is empty. You can set the timeout argument if block is True. Get blocks if the queue is empty after the number of seconds specified by timeout and a Full exception is raised.
  • Queue.task_done – In scenarios, after processing a get item, calling task_done signals to the Queue that the task has completed (paired with queue.get).
  • Queue. Put (… [,block[,timeout]]) — Insert an item to the end of the queue. Similarly, if block=True, the queue will block when full and wait for an empty space to be put. As with timeout of GET, timeout of PUT is set to timeout when block is True.
  • ** queue.join – ** Monitors all items and blocks the main thread until task_done is called on all items. The nice thing about this is, if a thread starts processing the last task, it takes the last task off the queue, and the queue is empty but the last thread hasn’t finished processing it yet. When a JOIN is called, the main thread does not terminate because the queue is empty, but instead waits for the last thread to finish processing.

The Python thread fifO Queue is used

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread fifO Queue. Py @time :2021/05/04 07:37 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" import threading import queue q = queue.Queue(5) def put(): for I in range(20): Print ('ok') def get(): print('ok') def get(): for i in range(20): Value = q.get_done () print(" number %d "% value) q.task_done() The # counter is the same as the number of numbers put. For each task_done task, the counter is decreased by 1. T1 = threading.Thread(target=put, args=()) t1. Start () t2 = threading.Thread(target=get, args=()) Args =()) t2.start() 0 in number 1 in queue queue success successful number 2 in the queue number 3 in the queue successful number 4 in queue number 0 heavy queue to retrieve the number 1 queue to retrieve the number 2 to take out the heavy queue to retrieve the number 3 the queue into the number 5 out of number four heavy queue queue successful number 6 in the queue Digit 7 Is saved to the queue successfully digit 8 Is saved to the queue successfully Digit 9 Is saved to the queue successfully Digit 5 Is removed from the queue successfully digit 6 Is removed from the queue successfully digit 7 Is removed from the queue successfully digit 9 Is removed from the queue 10 Is saved to the queue successfully digit 11 Is saved to the queue successfully Digit 12 is saved to the queue successfully Number 13 Successfully saved to the queue 14 Successfully saved to the queue 10 Successfully removed from the requeue 11 Successfully removed from the requeue 12 Successfully removed from the requeue 15 Successfully saved to the queue 16 Successfully saved to the queue 17 Successfully saved to the queue 13 Successfully removed from the requeue 14 Successfully removed from the requeue 15 Successfully removed from the requeue Digit 16 Requeued digit 18 requeued digit 19 Requeued digit 17 Requeued digit 18 requeued digit 19 requeued digit 19 requeued digit 17 requeued digit 18 requeued digit 19 requeued digit ok ""Copy the code

Five.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python function declarations and calls
  5. Python variable argument *argc/**kargcs
  6. Python anonymous function lambda
  7. Python return logic determines expressions
  8. Python string/list/tuple/dictionary conversions
  9. Python local and global variables
  10. The Python type function is different from the isinstance function
  11. Python is differs from ==
  12. Python mutable and immutable data types
  13. Shallow and deep copies of Python
  14. Read and write Python files
  15. Python exception Handling
  16. Python module import
  17. Python __name__ == ‘__main__’ explained in detail
  18. Python thread creation and parameter passing
  19. Python thread mutex Lock
  20. Python thread time Event
  21. The Python thread Condition variable Condition
  22. Python thread Timer Timer
  23. Python thread Semaphore
  24. Python thread Barrier object Barrier
  25. Python thread Queue Queue – FIFO
  26. Python thread queue LifoQueue – LIFO
  27. Python thread PriorityQueue PriorityQueue

Python threads Queue — FIFO

This article is published by the blog – Ape Say Programming Ape Say programming!