Skip to main content

Router

Route tasks based on the :setting:task_routes setting.

Attributes

AttributeTypeDescription
app[Celery](../base/celery.md?sid=celery_app_base_celery)The Celery application instance associated with this router.
queuesdict = {}A mapping of queue names to queue configuration objects used to resolve destination details.
routeslist = []A list of routing rules or router objects used to determine the destination for a task.
create_missingboolean = falseBoolean flag indicating whether to automatically create queues that are not explicitly defined in the configuration.

Constructor

Signature

def Router(
routes: list|null = None,
queues: dict|null = None,
create_missing: boolean = False,
app: Celery|null = None
) - > null

Parameters

NameTypeDescription
routes`listnull` = None
queues`dictnull` = None
create_missingboolean = FalseWhether to automatically create queues that are not explicitly defined.
app`Celerynull` = None

Signature

def Router(
routes: list = None,
queues: dict = None,
create_missing: boolean = False,
app: [Celery](../base/celery.md?sid=celery_app_base_celery) = None
) - > null

Parameters

NameTypeDescription
routeslist = NoneA list of routing rules or router objects used to determine task destinations
queuesdict = NoneA mapping of queue names to their respective configuration objects
create_missingboolean = FalseWhether to automatically create queues that are not explicitly defined in the configuration
app[Celery](../base/celery.md?sid=celery_app_base_celery) = NoneThe Celery application instance associated with this router

Methods


route()

@classmethod
def route(
options: dict,
name: string,
args: tuple = (),
kwargs: dict = None,
task_type: Any = None
) - > dict

Determines the final routing destination for a task by merging provided options with matching route definitions.

Parameters

NameTypeDescription
optionsdictThe initial execution options provided by the caller
namestringThe registered name of the task being routed
argstuple = ()Positional arguments intended for the task execution
kwargsdict = NoneKeyword arguments intended for the task execution
task_typeAny = NoneThe task class or type object associated with the task name

Returns

TypeDescription
dictA dictionary containing the resolved routing options, including the target queue and exchange details

expand_destination()

@classmethod
def expand_destination(
route: string | dict
) - > dict

Resolves a routing identifier or partial route dictionary into a full route configuration by looking up the associated queue.

Parameters

NameTypeDescription
route`stringdict`

Returns

TypeDescription
dictA dictionary where the 'queue' key is populated with a valid Queue object

lookup_route()

@classmethod
def lookup_route(
name: string,
args: tuple = None,
kwargs: dict = None,
options: dict = None,
task_type: Any = None
) - > dict | null

Iterates through the registered routers to find the first matching route for a specific task.

Parameters

NameTypeDescription
namestringThe name of the task to look up in the routing table
argstuple = NonePositional arguments of the task used for content-based routing
kwargsdict = NoneKeyword arguments of the task used for content-based routing
optionsdict = NoneCurrent execution options that may influence the routing decision
task_typeAny = NoneThe task class or type object being routed

Returns

TypeDescription
`dictnull`

query_router()

@classmethod
def query_router(
router: callable | object,
task: string,
args: tuple,
kwargs: dict,
options: dict,
task_type: Any
) - > dict | null

Executes a single router's logic to retrieve routing information, supporting both legacy class-based and modern callable routers.

Parameters

NameTypeDescription
router`callableobject`
taskstringThe name of the task being routed
argstupleThe positional arguments for the task
kwargsdictThe keyword arguments for the task
optionsdictThe task execution options
task_typeAnyThe task class or type object

Returns

TypeDescription
`dictnull`