Python Queues Queue module

Source: Internet
Author: User

the queue module in Python is commonly used to handle queuing-related issuesqueues are often used in producer consumer models, with the main function of improving efficiency and decoupling programs

1. Basic use of the queue module and related instructions

#-*-coding:utf-8-*-#Author:wong Du" "queues are often used in producer consumer models, with the main function of improving efficiency and decoupling programs" "ImportQueue"""three ways to instantiate different rules for a queue object"""Q1= Queue. Queue (maxsize=2)#first in, first outQ2 = Queue. Lifoqueue (maxsize=3)#in-First outQ3 = queue. Priorityqueue (maxsize=5)#determines who is first out of the queue based on the priority of the stored data"""add data into the queue, you can add str, list, tuple, etc. when the amount of data added exceeds the upper limit of the queue, the program will get stuck until someone pulls the data out of the queue. If you want the program not to get stuck, you can use Put_ NoWait adding data and configuring the put parameter of block or timeout to cause the program to throw an exception for exception handling or other operations"""Q1.put ("Caiyun") Q1.put ([1, 2, 3, 4, 5] )#q1.put_nowait (2)#q1.put (2, Block=false)#q1.put (2, timeout=3)Q2.put ("Caiyun") Q2.put (1, 2, 3, 4, 5)) Q3.put ("Wong", 123)) Q3.put ("Caiyun", 322)) Q3.put ("Dudu", 98))"""get the data in the queue, when there is no data in the queue, the program will be stuck until someone adds data in the queue if you want the program not to get stuck, you can use get_nowait to add data and configure the put parameter of block or timeout to throw an exception to the program. For exception handling or other operations"""Print("\033[32;1mqueue info\033[0m". Center (35,'-'))Print(Q1.get ())Print(Q1.get ())#q1.get_nowait ()#q1.get (Block=false)#q1.get (timeout=3)Print("\033[33;1mlifoqueue info\033[0m". Center (35,'-'))Print(Q2.get ())Print(Q2.get ())Print("\033[34;1mpriorityqueue info\033[0m". Center (35,'-'))Print(Q3.get () [0])Print(Q3.get ())#print (Q3.get ())"""Queue judge and count, determine if empty, full, queue Length count"""Print(Q1.empty ())Print(Q1.full ())Print(Q3.qsize ())

2. Simple application of the queue module

1 #-*-coding:utf-8-*-2 #Author:wong Du3 4 Import Time5 ImportQueue6 ImportThreading7 8Q = queue. Queue (maxsize=10)9 Ten defproducer (pname): OneCount = 1 A      whileTrue: -Q.put ("baozi%s"%count) -         Print("\033[31;1m[%s] produced [baozi%s]...\033[0m"%(PName, count)) theCount + = 1 -Time.sleep (0.5) -  - defconsumer (CNAME): +      whileTrue: -         Print("\033[33;1m[%s] received [%s] and ate it ... \033[0m"%(CNAME, Q.get ())) +Time.sleep (2) A  atP1 = Threading. Thread (Target=producer, args= ("Caiyun", )) - P1.start () -  -C1 = Threading. Thread (Target=consumer, args= ("Dudu", )) -C2 = Threading. Thread (Target=consumer, args= ("Wong", )) - C1.start () in #C2.start ()
Queue&threading_ Producer Consumer example

Python Queues Queue module

Related Article

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.