Skip to content

Processing priorities

Priority-based event routing for production and backfill lanes. Events can be routed to different processing streams based on priority, enabling bulk migrations without disrupting real-time event processing.

See Priority Lanes for the conceptual explanation.

Priority

Bases: IntEnum

Message processing priority levels.

A higher value means a higher priority, and the outbox publishes higher-priority messages first. Messages that share a partition key still publish in commit order, whatever their priority. When priority lanes are enabled, a value below server.priority_lanes.threshold (default 0) routes the event to the backfill lane, which the engine reads only when the primary lane is empty.

ATTRIBUTE DESCRIPTION
BULK

Bulk imports, re-indexing and mass data operations. Backfill lane.

LOW

Background tasks, data migrations and non-urgent work. Backfill lane.

NORMAL

The default for production traffic. Primary lane.

HIGH

Time-sensitive operations. Primary lane.

CRITICAL

System-critical operations such as payments. Primary lane.


processing_priority

processing_priority(
    priority: Priority | int,
) -> Generator[None, None, None]

Set the processing priority for every command processed inside the block.

Events raised by those commands carry this priority. See Priority for how the value affects lane routing and outbox order.

Contexts can be nested, and the innermost one wins. The previous priority is restored when the block exits, even when it raises.

PARAMETER DESCRIPTION
priority

A Priority member or a plain integer.

TYPE: Priority | int

Example

with processing_priority(Priority.LOW): ... domain.process(CreateCustomer(name="Migration User"))

Source code in src/protean/utils/processing.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@contextmanager
def processing_priority(priority: Priority | int) -> Generator[None, None, None]:
    """Set the processing priority for every command processed inside the block.

    Events raised by those commands carry this priority. See ``Priority`` for
    how the value affects lane routing and outbox order.

    Contexts can be nested, and the innermost one wins. The previous priority
    is restored when the block exits, even when it raises.

    Args:
        priority: A ``Priority`` member or a plain integer.

    Example:
        >>> with processing_priority(Priority.LOW):
        ...     domain.process(CreateCustomer(name="Migration User"))
    """
    token = _processing_priority.set(int(priority))
    try:
        yield
    finally:
        _processing_priority.reset(token)

current_priority

current_priority() -> int

Get the current processing priority from context.

Returns the priority set by the nearest enclosing processing_priority() context manager, or Priority.NORMAL (0) if no context is active.

RETURNS DESCRIPTION
int

The current priority value.

TYPE: int

Example

current_priority() 0 with processing_priority(Priority.LOW): ... current_priority() -50

Source code in src/protean/utils/processing.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def current_priority() -> int:
    """Get the current processing priority from context.

    Returns the priority set by the nearest enclosing ``processing_priority()``
    context manager, or ``Priority.NORMAL`` (0) if no context is active.

    Returns:
        int: The current priority value.

    Example:
        >>> current_priority()
        0
        >>> with processing_priority(Priority.LOW):
        ...     current_priority()
        -50
    """
    return _processing_priority.get()