Python entry thread

Source: Internet
Author: User

Queue

The queue is similar to a pipe, the element is first out, in put(arg) ,get()
One thing to note is that the queue is in-memory operation, the process exits, the queue is emptied , and the queue is a blocking pattern.

Queue classification

Queues have many, but are dependent on modulesqueue
| Queue Mode | features |
|---|---|
|queue. Queue| Advanced first-out queue |
|queue. Lifoqueue| LIFO Queue |
|queue. Priorityqueue| Priority Queue |
|queue.deque| Dual Line Queue |

Methods of the queue
Method Usage Notes
Put Data, Queue.put () default has block=true and timeout two parameters. When Block=true, the write is blocked and the blocking time is determined by timeout. When the queue q is filled (other threads), the code blocks until the other thread takes the data. The Queue.put () method, combined with the Block=false parameters, solves this hidden problem. Note, however, that non-blocking write queues will throw exception Queue.full exceptions when the queue is full
Get Fetch data (default blocking), Queue.get ([block[, timeout]]) Get queue, timeout wait time
Empty Returns true if the queue is empty, otherwise false
Qsize Displays the length of the element that is real in the queue
MaxSize Maximum supported queue length, without parentheses when used
Join It actually means waiting until the queue is empty and then doing something else
Task_done After completing a work, the Queue.task_done () function sends a signal to the queue that the task has completed
Full Returns true if the queue is full, and vice versa false
One-way queues
import queueq=queue.Queue(5)    #如果不设置长度,默认为无限长print(q.maxsize) #注意没有括号q.put(123)q.put(456)q.put(789)q.put(100)q.put(111)q.put(233)print(q.get())print(q.get())

When this is printed it is blocked, for what, because 5 elements of the length of the queue, but I put in 6, so I blocked. If you write less, you can show the correct one 123 .

LIFO queue
= queue.LifoQueue()q.put(12)q.put(34)print(q.get())
Priority queue

It is important to note that the priority queue put is a tuple, (priority, data), the lower the priority level, the higher the rank

= queue.PriorityQueue()q.put((3,‘aaaaa‘))q.put((3,‘bbbbb‘))q.put((1,‘ccccc‘))q.put((3,‘ddddd‘))print(q.get())print(q.get())

Out

(1, ‘ccccc‘)(3, ‘aaaaa‘)
Dual Line Queue
= queue.deque()q.append(123)q.append(456)q.appendleft(780)print(q.pop())print(q.popleft())

Out

456780
Production consumer models to solve what problems, use scenarios

From the figure below, it can be seen that producers and consumers are strung together in a similar queue. The queue can be imagined as a "warehouse" for the product, and the producer needs to be concerned with the "warehouse", not the specific consumer, or even the presence of these consumers for the producers. For consumers, he also does not need to care about the specific producers, in the end there are many producers is not his concern, he just care about the "warehouse" there is nothing. This model is a loosely coupled model. This will answer the first question I raised above. This model is created to reuse and decouple. For example, a common message framework (a very classic usage scenario for a producer consumer model) is ActiveMQ. The sending and receiving ends are associated with topic. This topic can be understood as the address of our "warehouse" here, so that we can achieve the point-to-point and broadcast two ways to distribute the message.

A word summary

Solution decoupling, less resources to solve high concurrency problems

Multiprocessing Module

Multiprocessing is a, supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency,effectively side-stepping the Global interpreter Lock By using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

Because of the Gil's existence, multithreading in Python is not really multi-threading, and if you want to fully use the resources of multicore CPUs, most of the situations in Python need to use multiple processes.

The multiprocessing package is a multi-process Management Pack in Python. With threading. Like thread, it can take advantage of multiprocessing. Processes object to create a process. The process can run functions written inside the Python program. The process object is used in the same way as the thread object, and there is a method for start (), run (), join (). There are also lock/event/semaphore/condition classes in the multiprocessing package (these objects can be passed to each process as multi-threaded, through parameters) to synchronize the process, using the same class as the threading package. So, a large part of multiprocessing and threading use the same set of APIs, only to a multi-process situation.

Python entry thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.