guv.queue - greenthread-compatible queue

Synchronized queues

This module implements multi-producer, multi-consumer queues that work across greenlets, with the API similar to the classes found in the standard queue and multiprocessing modules.

A major difference is that queues in this module operate as channels when initialized with maxsize of zero. In such case, both empty() and full() return True and put() always blocks until a call to get() retrieves the item.

An interesting difference, made possible because of GreenThreads, is that qsize(), empty(), and full() can be used as indicators of whether the subsequent get() or put() will not block. The new methods LightQueue.getting() and LightQueue.putting() report on the number of GreenThreads blocking in put() or get() respectively.

exception guv.queue.Full[source]

Bases: Exception

Exception raised by Queue.put(block=0)/put_nowait().

exception guv.queue.Empty[source]

Bases: Exception

Exception raised by Queue.get(block=0)/get_nowait().

class guv.queue.Queue(maxsize=None)[source]

Bases: guv.queue.LightQueue

Create a queue object with a given maximum size

If maxsize is less than zero or None, the queue size is infinite.

Queue(0) is a channel, that is, its put() method always blocks until the item is delivered. (This is unlike the standard queue.Queue, where 0 means infinite size).

In all other respects, this Queue class resembles the standard library, queue.Queue.

join() → None[source]

Block until all items in the queue have been gotten and processed

The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

task_done() → None[source]

Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put into the queue).

Raises a ValueError if called more times than there were items placed in the queue.

class guv.queue.PriorityQueue(maxsize=None)[source]

Bases: guv.queue.Queue

A subclass of Queue that retrieves entries in priority order (lowest first)

Entries are typically tuples of the form: (priority number, data).

class guv.queue.LifoQueue(maxsize=None)[source]

Bases: guv.queue.Queue

A subclass of Queue that retrieves most recently added entries first

class guv.queue.LightQueue(maxsize=None)[source]

Bases: object

This is a variant of Queue that behaves mostly like the standard Queue. It differs by not supporting the task_done() or join() methods, and is a little faster for not having that overhead.

empty() → None[source]

Return True if the queue is empty, False otherwise.

full() → None[source]

Return True if the queue is full, False otherwise.

Queue(None) is never full.

get(block=True, timeout=None) → None[source]

Remove and return an item from the queue.

If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the queue.Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the queue.Empty exception (timeout is ignored in that case).

get_nowait() → None[source]

Remove and return an item from the queue without blocking.

Only get an item if one is immediately available. Otherwise raise the queue.Empty exception.

getting() → None[source]

Returns the number of GreenThreads that are blocked waiting on an empty queue.

put(item, block=True, timeout=None) → None[source]

Put an item into the queue.

If optional arg block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the queue.Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the queue.Full exception (timeout is ignored in that case).

put_nowait(item) → None[source]

Put an item into the queue without blocking.

Only enqueue the item if a free slot is immediately available. Otherwise raise the queue.Full exception.

putting() → None[source]

Returns the number of GreenThreads that are blocked waiting to put items into the queue.

qsize() → None[source]

Return the size of the queue.

resize(size) → None[source]

Resizes the queue’s maximum size.

If the size is increased, and there are putters waiting, they may be woken up.