Python queues queue

Source: Internet
Author: User

Basic FIFO queue: FIFO first.

CALSS Queue.queue (maxsize=0)

MaxSize is an integer that indicates the maximum number of data that can be stored in a queue. Once the upper limit is reached, the insertion causes blocking until the data in the queue is consumed.

#coding: Utf-8import queueq=queue.queue (5) #队列中只能存放5个数据for I in range (5):    q.put (i) and not Q.empty ():    print Q.get ()
LIFO queue: LIFO

Class Queue.lifoqueue (maxsize=0)

#coding: Utf-8import queueq=queue.lifoqueue (5) #队列中只能存放5个数据for I in range (5):    q.put (i) and not Q.empty ():    Print Q.get ()    

Results:

4
3
2
1
0

Priority queue

Class Queue.priorityqueue (maxsize=0)

Import Queueimport Threadingclass Job (object):    def __init__ (self, Priority, description):        self.priority = Priority        self.description = description        print ' Job: ', description        return    def __cmp__ (self, Other):        return CMP (self.priority, other.priority) q = Queue.priorityqueue () q.put (Job (3, ' Level 3 job ')) q.put (Job (10, ' Level Jobs ') q.put (Job (1, ' Level 1 job ')) def process_job (q): When    True:        next_job = Q.get ()        print ' for: ', Next_job.description        q.task_done () workers = [Threading. Thread (Target=process_job, args= (Q,)),        Threading. Thread (Target=process_job, args= (Q,))        ]for W in workers:    W.setdaemon (True) W.start () Q.join (    )
Common methods:

Queue.qsize()

Queue.empty()

Queue.full()

Queue.put(item, block=true, timeout=none)

Queue.put_nowait(item)

Queue.get(block=true, timeout=none)

Queue.get_nowait()

Queue.task_done()

Queue.join()

Producer Consumer Model

Python queues queue

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.