Python multithreaded (3)--queue module

Source: Internet
Author: User

The queue module supports FIFO (FIFO) queuing, which supports multi-threaded access, including a primary type (Queue) and two exception classes (exception classes).

The queue module in Python 2 was renamed queue in Python 3.

Creation of a Queue object

You can obtain a queue object by instantiating the queue type:

Q = Queue.queue (maxsize=0)

To create a new queue, the meaning of the parameter maxsize is:

    • If maxsize > 0: When the element in Q reaches MaxSize, the queue is full, and when another thread wants to insert into it, if the block option is specified, it will block until a thread pulls an element out of it.
    • If MaxSize <= 0:python would consider this to be a queue without capacity constraints.

Exception class defined by the queue module

Queue.empty

If the queue q is empty and Q.get (False) is called at this point, the exception is thrown.

Queue.full

If the queue q is full and the Q.put (x, False) is called, the exception is thrown.

Method of the Queue object

Q.empty ()

Determines whether the queue is empty.

Q.full ()

Determines whether the queue is full.

Q.get (block=true, timeout=None) q.get_nowait ()

Parameter timeout is meaningless when the parameter block is False because the thread does not block:

    • If the queue is not empty, take away and return the element;
    • If the queue is empty, throw queue.empty

  When block is True, the time -out timeout is determined when the queue is empty, the process is blocked, or the process is blocked for some time.

Get_nowait () equals get (False), or get (timeout=0) does not block waiting, regardless of whether the queue is empty or not.

For example:

Try:    x = q.get_nowait () except Queue.empty:    print "No more items to process"

  

Q.put (item, block=true, timeout=None) q.put_nowait (item)

Inserts an item into the queue if the queue is full, throws a queue.full, or waits for a thread to block.

Q.qsize ()

Returns the number of elements in the current queue.

Q.join ()

Q.task_done ()

Python multithreaded (3)--queue module

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.