Python learning notes: queue, producer consumer model

1: Queue:

1. Definition: a queue can be understood as a container. There is a sequence inside the container, similar to a linked list and a stack. A queue is also a structure for storing data. The order in which data enters the queue is very important. Generally speaking, a queue is a group of people or things waiting for service or processing in the order they are arranged

2. Function:

a. decoupling between programs (reducing code dependency): if queues are added between the front end and the back end, the front end only needs to throw data into the queues, and does not need to care about how the back end generates data. The expansion of the number of front end users is also independent of the back end, and the expansion of the data processing capacity of the back end is independent of the front end.

b. improve the efficiency of program execution: the front end does not need to wait for the back end to finish executing, but only needs to put the data into the queue, and the queue executes itself. During this period, you can do other things, and return to get the results.

3. What is the difference between queue and list?

Why queue when there are lists? The list can also be used as a container. The difference is that the list data fetching is equivalent to copying a copy of the fetching. Unless the data is deleted manually, it will still be in the list, and the queue fetching one will be deleted automatically.

4. Queue code:

import queue

import queue

# Queues with priority, the smaller the number, the higher the priority
q = queue.PriorityQueue()
q.put((-1, "car1"))
q.put((3, "car2"))
q.put((10, "car3"))
q.put((6, "car4"))
print(q.get())
print(q.get())
print(q.get())
print(q.get())
# First in first out
q = queue.Queue()
q.put("1")
q.put("2")
q.put("3")
print(q.get())
print(q.get())
print(q.get())

# Last in, first out. There are also examples in life. For example, buying fruit must be buying advanced and fresh fruit
q = queue.LifoQueue()
q.put(1)
q.put(2)
q.put(3)
print(q.get())
print(q.get())
print(q.get())
# If get The number of times has exceeded the number of elements in the queue, then the program will enter the wait, waiting for new elements in the queue to enter (another thread is required to add elements),
# If you do not want the program to enter the wait, you can use the get_nowait perhaps get(block=False),Throw if the queue is empty Empty Exception, or judge queue element empty No get
if not q.empty():
    print("Take elements out of the queue:", q.get())
# print("...1",q.get(block=False))
# print("...2",q.get_nowait())

 

2, Producer consumer model:

1. Definition: this model solves the problem of strong coupling between data producers and consumers through a container. There is no direct communication between producers and consumers, but communication through blocking queues. After the producers generate data, they do not need to wait for consumers. They only need to put the data in the queues, and consumers do not directly find the producers for data, but directly get it in the queues Columns act as a buffer, balancing the processing power of production and consumers.

# Production consumption model
import queue
import threading
import time

# After the production of 10, the production thread is blocked, and the production will continue only after the consumer consumes one
q = queue.Queue(maxsize=10)


# producer
def Producer(name):
    count = 1
    while True:
        q.put("bone%s" % count)
        print(" A total of bones were produced:%s" % count," The current queues are:%s" % q.qsize())
        count += 1
        # The faster you produce, the faster you eat
        time.sleep(0.1)


# consumer
def Consumer(name):
    # while q.qsize()>0:
    while True:
        print("             [%s] Take to[%s] And ate it..." % (name, q.get()))
        time.sleep(1)


p = threading.Thread(target=Producer, args=("Producer 1",))
c = threading.Thread(target=Consumer, args=("User 1",))
c1 = threading.Thread(target=Consumer, args=("User 2",))

p.start()
c.start()
c1.start()

Tags: Python

Posted on Sun, 24 May 2020 10:07:43 -0400 by Ghostgator