Skip to main content
Queues are distributed, FIFO data structures for coordinating work across Modal Functions and external processes.

Creating queues

FromName

References a Queue by its name, optionally creating it if it doesn’t exist.
queue, err := mc.Queues.FromName(ctx, "my-queue", &modal.QueueFromNameParams{
    CreateIfMissing: true,
})
if err != nil {
    // Handle error
}
ctx
context.Context
required
Context for the operation
name
string
required
The name of the Queue
params
*QueueFromNameParams
Optional parameters
Environment
string
Environment name. Defaults to client’s environment.
CreateIfMissing
bool
If true, creates the Queue if it doesn’t exist. Defaults to false.
Returns:
  • *Queue - The Queue instance
  • error - NotFoundError if the Queue doesn’t exist and CreateIfMissing is false

Ephemeral

Creates a temporary, nameless Queue that persists until CloseEphemeral() is called.
queue, err := mc.Queues.Ephemeral(ctx, nil)
if err != nil {
    // Handle error
}
defer queue.CloseEphemeral()
ctx
context.Context
required
Context for the operation
params
*QueueEphemeralParams
Optional parameters
Environment
string
Environment name
Returns:
  • *Queue - The ephemeral Queue instance
  • error - Error if creation fails

Queue methods

Put

Adds a single item to the end of the Queue.
err := queue.Put(ctx, "Hello, Queue!", &modal.QueuePutParams{
    Partition: "partition-1",
})
if err != nil {
    // Handle error
}
ctx
context.Context
required
Context for the operation
v
any
required
The item to add (will be serialized with pickle)
params
*QueuePutParams
Optional parameters
Timeout
*time.Duration
Maximum wait time if Queue is full. nil = wait indefinitely.
Partition
string
Partition key (1-64 bytes). Empty string uses default partition.
PartitionTTL
time.Duration
Time-to-live for the partition. Defaults to 24 hours.
Returns:
  • error - QueueFullError if the Queue is full after timeout

PutMany

Adds multiple items to the end of the Queue.
err := queue.PutMany(ctx, []any{"item1", "item2", "item3"}, nil)
if err != nil {
    // Handle error
}
ctx
context.Context
required
Context for the operation
values
[]any
required
The items to add
params
*QueuePutManyParams
Same parameters as Put
Returns:
  • error - QueueFullError if the Queue is full after timeout

Get

Removes and returns one item from the Queue.
item, err := queue.Get(ctx, &modal.QueueGetParams{
    Timeout:   ptr(5 * time.Second),
    Partition: "partition-1",
})
if err != nil {
    // Handle QueueEmptyError
}
fmt.Printf("Item: %v\n", item)
ctx
context.Context
required
Context for the operation
params
*QueueGetParams
Optional parameters
Timeout
*time.Duration
Maximum wait time for an item. nil = wait indefinitely.
Partition
string
Partition key to get from
Returns:
  • any - The dequeued item
  • error - QueueEmptyError if no items available within timeout

GetMany

Removes and returns up to n items from the Queue.
items, err := queue.GetMany(ctx, 10, &modal.QueueGetManyParams{
    QueueGetParams: modal.QueueGetParams{
        Timeout: ptr(5 * time.Second),
    },
})
if err != nil {
    // Handle error
}
fmt.Printf("Got %d items\n", len(items))
ctx
context.Context
required
Context for the operation
n
int
required
Maximum number of items to retrieve
params
*QueueGetManyParams
Same parameters as Get
Returns:
  • []any - The dequeued items (may be fewer than n)
  • error - QueueEmptyError if no items available within timeout

Len

Returns the number of items in the Queue.
length, err := queue.Len(ctx, &modal.QueueLenParams{
    Partition: "partition-1",
})
if err != nil {
    // Handle error
}
fmt.Printf("Queue length: %d\n", length)
ctx
context.Context
required
Context for the operation
params
*QueueLenParams
Optional parameters
Partition
string
Partition to check. Empty for default partition.
Total
bool
If true, returns total across all partitions (mutually exclusive with Partition)
Returns:
  • int - The number of items
  • error - Error if the request fails

Clear

Removes all items from a Queue partition.
err := queue.Clear(ctx, &modal.QueueClearParams{
    Partition: "partition-1",
})
if err != nil {
    // Handle error
}
ctx
context.Context
required
Context for the operation
params
*QueueClearParams
Optional parameters
Partition
string
Partition to clear. Empty for default partition.
All
bool
If true, clears all partitions (mutually exclusive with Partition)
Returns:
  • error - Error if the operation fails

Iterate

Yields items from the Queue until it is empty.
for item, err := range queue.Iterate(ctx, &modal.QueueIterateParams{
    ItemPollTimeout: 10 * time.Second,
}) {
    if err != nil {
        // Handle error
        break
    }
    fmt.Printf("Item: %v\n", item)
}
ctx
context.Context
required
Context for the operation
params
*QueueIterateParams
Optional parameters
ItemPollTimeout
time.Duration
Exit iteration if no new items appear within this timeout
Partition
string
Partition to iterate over
Returns:
  • iter.Seq2[any, error] - Iterator over Queue items

CloseEphemeral

Deletes an ephemeral Queue.
queue.CloseEphemeral()
This method panics if called on a non-ephemeral Queue.

Managing queues

Delete

Deletes a named Queue.
err := mc.Queues.Delete(ctx, "my-queue", &modal.QueueDeleteParams{
    AllowMissing: false,
})
if err != nil {
    // Handle error
}
ctx
context.Context
required
Context for the operation
name
string
required
The name of the Queue to delete
params
*QueueDeleteParams
Optional parameters
Environment
string
Environment name
AllowMissing
bool
If true, don’t error if the Queue doesn’t exist
Returns:
  • error - Error if deletion fails
Deletion is irreversible and will affect any Apps currently using the Queue.

Queue type

QueueID
string
The unique identifier for the Queue
Name
string
The name of the Queue (empty for ephemeral Queues)

Example usage

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/modal-labs/modal-client/go"
)

func main() {
    ctx := context.Background()
    mc, _ := modal.NewClient()
    defer mc.Close()

    // Create a Queue
    queue, err := mc.Queues.FromName(ctx, "my-queue", &modal.QueueFromNameParams{
        CreateIfMissing: true,
    })
    if err != nil {
        panic(err)
    }

    // Put items
    queue.Put(ctx, "task-1", nil)
    queue.Put(ctx, "task-2", nil)
    queue.PutMany(ctx, []any{"task-3", "task-4", "task-5"}, nil)

    // Check length
    length, _ := queue.Len(ctx, nil)
    fmt.Printf("Queue has %d items\n", length)

    // Get items
    timeout := 5 * time.Second
    item, err := queue.Get(ctx, &modal.QueueGetParams{
        Timeout: &timeout,
    })
    if err != nil {
        fmt.Printf("Queue empty: %v\n", err)
    } else {
        fmt.Printf("Got item: %v\n", item)
    }

    // Get multiple items
    items, _ := queue.GetMany(ctx, 10, nil)
    fmt.Printf("Got %d items\n", len(items))

    // Iterate over items
    for item, err := range queue.Iterate(ctx, nil) {
        if err != nil {
            break
        }
        fmt.Printf("Processing: %v\n", item)
    }

    // Use partitions
    queue.Put(ctx, "data-1", &modal.QueuePutParams{
        Partition: "batch-1",
    })
    item2, _ := queue.Get(ctx, &modal.QueueGetParams{
        Partition: "batch-1",
    })
    fmt.Printf("From partition: %v\n", item2)
}

Build docs developers (and LLMs) love