Skip to content

Context class

This page documents the Context class of FluxQueue.

You can import the Context class from fluxqueue:

from fluxqueue import Context

fluxqueue.Context

Context()

Base execution context for FluxQueue tasks.

Provides a dual-layer storage system designed for high-performance distributed task execution:

  • Worker Layer (thread_storage): Persists across the lifetime of an individual worker thread. Use this for heavy resource pooling (e.g., DB engines, HTTP clients) to avoid re-initialization overhead.
  • Task Layer (metadata): Isolated to a single task execution via ContextVars. Provides read-only access to the current task's unique identity and execution state.

This class can be used directly for basic metadata access or subclassed to provide domain-specific resources.

Source code in python/fluxqueue/context.py
def __init__(self) -> None:
    self._thread_local = threading.local()
    self._metadata_var: ContextVar[TaskMetadata] = ContextVar("task_metadata")

thread_storage property

thread_storage

Retrieves the thread-persistent storage dictionary.

Returns a dictionary that persists across all tasks executed by the current worker. Used for storing long-lived resources like database engines and connection pools to avoid re-initialization overhead.

metadata property

metadata

Returns metadata isolated to the current task.

Returns a TaskMetadata instance containing execution details like retry counts and task IDs. This property uses ContextVars to ensure data isolation during concurrent task execution on the same thread.