ray.util.queue.Queue#

class ray.util.queue.Queue(maxsize: int = 0, actor_options: Dict | None = None)[source]#

A first-in, first-out queue implementation on Ray.

The behavior and use cases are similar to those of the asyncio.Queue class.

Features both sync and async put and get methods. Provides the option to block until space is available when calling put on a full queue, or to block until items are available when calling get on an empty queue.

Optionally supports batched put and get operations to minimize serialization overhead.

Parameters:
  • maxsize (optional, int) – maximum size of the queue. If zero, size is unbounded.

  • actor_options (optional, Dict) – Dictionary of options to pass into the QueueActor during creation. These are directly passed into QueueActor.options(…). This could be useful if you need to pass in custom resource requirements, for example.

Examples

from ray.util.queue import Queue
q = Queue()
items = list(range(10))
for item in items:
    q.put(item)
for item in items:
    assert item == q.get()
# Create Queue with the underlying actor reserving 1 CPU.
q = Queue(actor_options={"num_cpus": 1})

PublicAPI (beta): This API is in beta and may change before becoming stable.

Methods

empty

Whether the queue is empty.

full

Whether the queue is full.

get

Gets an item from the queue.

get_async

Gets an item from the queue.

get_nowait

Equivalent to get(block=False).

get_nowait_batch

Gets items from the queue and returns them in a list in order.

put

Adds an item to the queue.

put_async

Adds an item to the queue.

put_nowait

Equivalent to put(item, block=False).

put_nowait_batch

Takes in a list of items and puts them into the queue in order.

qsize

The size of the queue.

shutdown

Terminates the underlying QueueActor.

size

The size of the queue.