The Queue Module

The Queue module supplies first-in, first-out (FIFO) queues that support multithread access, with one main class and two exception classes.

Queue

class Queue(maxsize=0)

Queue is the main class for module Queue and is covered in Methods of Queue Instances. When maxsize is greater than 0, the new Queue instance q is deemed full when q has maxsize items. A thread inserting an item with the block option, when q is full, suspends until another thread extracts an item. When maxsize is less than or equal to 0, q is never considered full and is limited in size only by available memory, like normal Python containers.

Empty

Empty is the class of the exception that q.get(False) raises when q is empty.

Full

Full is the class of the exception that q.put(x,False) raises when q is full.

Methods of Queue Instances

An instance q of class Queue supplies the following methods.

empty

q.empty( )

Returns True if q is empty; otherwise, False.

full

q.full( )

Returns True if q is full; otherwise, False.

get, get_nowait

q.get(block=True,timeout=None)

When block is False, get removes and returns an item from q if one is available; otherwise, get raises Empty. When block is True and timeout is None, get removes and returns an item from q, suspending the calling thread, if need be, until an item is available. When block is True and timeout is not None, timeout must be a number >=0 (which may include a fractional part to specify a fraction of a second), and get waits for no longer than timeout seconds (if ...

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.