Queue is a distributed, FIFO (first-in, first-out) queue for data flow in Modal apps. The queue can contain any object serializable by cloudpickle, including Modal objects.
Key concepts
FIFO ordering: By default, the Queue acts as a single FIFO queue supporting puts and gets (blocking and non-blocking). Partitions: You can specify partition keys to access independent FIFO queues within the sameQueue object. Across any two partitions, puts and gets are completely independent.
Lifetime: By default, each partition is cleared 24 hours after the last put operation. A lower TTL can be specified. On app completion or after stopping an app, all partitions are cleared.
Limits: A single Queue can contain up to 100,000 partitions, each with up to 5,000 items. Each item can be up to 1 MiB. Partition keys must be non-empty and must not exceed 64 bytes.
Queues are best used for communication between active functions and should not be relied on for persistent storage.
Basic usage
Creating queues
Reference by name
Create or reference a named queue that persists:Ephemeral queues
Create a temporary queue that exists only within a context manager:Create from ID
Reference a queue by its object ID:Queue operations
Put items
Add items to the queue:Blocking behavior
By default,put blocks if the queue is full:
Get items
Remove and return items from the queue:Blocking behavior
Iterate over items
Iterate through items without removing them:Check queue length
Clear queue
Partitions
Partitions provide independent FIFO queues within the same Queue object.Basic partition usage
Partition TTL
Set custom expiration time for partitions:Partition operations
Producer-consumer pattern
Use queues to coordinate work between functions:Async usage
Use.aio suffix for async contexts:
Managing queues
TheQueue.objects namespace provides methods for managing named queues.
Create a queue
List queues
List all queues in the active environment:Delete a queue
API reference
Queue methods
Add an object to the end of the queue.Parameters:
v(Any): Value to addblock(bool): If True, wait for space. Default: Truetimeout(float, optional): Max seconds to wait if blockingpartition(str, optional): Partition keypartition_ttl(int): Seconds until partition expires. Default: 86400 (24 hours)
queue.Full if queue is full and can’t add itemAdd several objects to the end of the queue.Parameters:
vs(list[Any]): Values to addblock(bool): If True, wait for space. Default: Truetimeout(float, optional): Max seconds to wait if blockingpartition(str, optional): Partition keypartition_ttl(int): Seconds until partition expires. Default: 86400 (24 hours)
queue.Full if queue is full and can’t add itemsRemove and return the next object in the queue.Parameters:
block(bool): If True, wait for an item. Default: Truetimeout(float, optional): Max seconds to wait if blockingpartition(str, optional): Partition key
queue.Empty if timeout reached while blockingRemove and return up to
n_values objects from the queue.Parameters:n_values(int): Maximum number of items to getblock(bool): If True, wait for at least one item. Default: Truetimeout(float, optional): Max seconds to wait if blockingpartition(str, optional): Partition key
queue.Empty if timeout reached while blockingIterate through items in the queue without mutation (read-only).Parameters:
partition(str, optional): Partition keyitem_poll_timeout(float): Seconds to wait for next item. Default: 0.0
Return the number of objects in the queue.Parameters:
partition(str, optional): Partition keytotal(bool): If True, return total across all partitions. Default: False
Clear the contents of a partition or all partitions.Parameters:
partition(str, optional): Partition key to clearall(bool): If True, clear all partitions. Default: False
Return information about the Queue object. Returns a
QueueInfo dataclass with name, created_at, and created_by fields.Returns: QueueInfoError handling
Queue full
Queue empty
Request size errors
Best practices
- Set timeouts when using blocking operations to avoid indefinite waits
- Use partitions to organize different types of work or data streams
- Configure partition TTL based on your data retention needs
- Handle Empty and Full exceptions appropriately in your application logic
- Use
.aiomethods in async contexts to avoid blocking the event loop - Batch with
put_many/get_manyfor better performance when handling multiple items - Don’t rely on queues for long-term storage - they’re designed for active communication, not persistence