041 queue (important, multithreading), 041 queue

Source: Internet
Author: User

041 queue (important, multithreading), 041 queue

Content: queue type, method, and usage

###############
Queue defines three types of information queuing Modes
Queue ([maxsize]): FIFO Queue Mode
LifoQueue ([maxsize]): LIFO queuing mode, stack Mode


###### Method
Q. qsize (): returns the queue size.
Q. full (): If the queue is full, True is returned; otherwise, False is returned.
Q. empty (): True is returned if the queue is empty; otherwise, False is returned.


Queue features: first-in-first-out, with a lock inside to ensure the security of multi-threaded data

Import queueq = queue. queue (3) # set the number of data items to be stored in q. put ('jsoning') q. put ('xming') q. put ('qqqq') q. put ('Q', 0) # The default value of the parameter at 0 is, indicating that the thread is blocked when the number of settings is exceeded. 0 indicates that print (q. get () print (q. get () print (q. get () print (q. get (0) #0 indicates that no error is reported. The default value is blocking.
View Code

 

Purpose: you do not need to use synchronization. There is a lock in the queue.

Take the following example:

1 from random import randint 2 import threading 3 from time import sleep 4 import queue 5 6 class Production (threading. thread): 7 def run (self): 8 while True: 9 r = randint (0,100) 10 q. put (r) 11 print ("produced % s steamed stuffed bun" % r) 12 sleep (1) 13 14 class Proces (threading. thread): 15 def run (self): 16 while True: 17 ret = q. get () 18 print ('eat % s Steamed Stuffed Bun '% ret) 19 20 if _ name _ =' _ main _ ': 21 q = queue. queue () 22 threads = [Production (), Proces ()] 23 for t in threads: 24 t. start ()
View Code

 

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.