Python---Common data queues

Source: Internet
Author: User

FIFO Queue (first-in-one-out)

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Zhoufeng'#FIFO queueImportQueueq=queue. Queue (3)#define a 3-position queue#using the Put method to add elements to the queue, you need to consider whether you can drop the problem#if it does not fit, the default will be blocked (block=true), when blocking can define the time-out (timeout=5)#You can use the Block=false setting to not block immediate errorsQ.put (11) Q.put (22) Q.put (33)Print(Q.empty ())#determines whether the queue is empty, returns TRUE if NULL is returned false, NOT NULLPrint(Q.qsize ())#number of elements in output queueQ.put (44,block=true,timeout=5)#using the Get method to fetch data from a queue, you need to consider whether there is a problem with data that is desirable#If no data is desirable, the default is blocked (block=true), when blocking can define time-out (timeout=2)#You can use the Block=false setting to not block immediate errorsPrint(Q.get ())Print(Q.get ())Print(Q.get ())Print(Q.get (block=true,timeout=2))#There are also join and Task_done methods, both of which are used in pairs#The join method is used to block the process from continuing to execute, and when the task in the queue finishes, no longer blocks#How does the interpreter know that the task is done in the queue? Explicitly tells the interpreter that a task has been executed by means of the Task_done methodQue=queue. Queue (3)#defines a 3-position queue objectQue.put (11) Que.put (22) Que.put (33) Que.get () Que.task_done ()#tell the interpreter that a task has been executed.Que.get () que.task_done ( )#tell the interpreter that a task has been executed.Que.join ()
View Code

Second, Stack (last in first out)

Use queue. Lifoqueue () Build stack

Third, priority queue

Use queue. Priorityqueue () Generate priority queue

Four, two-way queue

Using Queue.deque () to generate a two-way queue

Python---Common data queues

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.