Router
Route tasks based on the :setting:task_routes setting.
Attributes
| Attribute | Type | Description |
|---|---|---|
| app | [Celery](../base/celery.md?sid=celery_app_base_celery) | The Celery application instance associated with this router. |
| queues | dict = {} | A mapping of queue names to queue configuration objects used to resolve destination details. |
| routes | list = [] | A list of routing rules or router objects used to determine the destination for a task. |
| create_missing | boolean = false | Boolean 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
| Name | Type | Description |
|---|---|---|
| routes | `list | null` = None |
| queues | `dict | null` = None |
| create_missing | boolean = False | Whether to automatically create queues that are not explicitly defined. |
| app | `Celery | null` = 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
| Name | Type | Description |
|---|---|---|
| routes | list = None | A list of routing rules or router objects used to determine task destinations |
| queues | dict = None | A mapping of queue names to their respective configuration objects |
| create_missing | boolean = False | Whether to automatically create queues that are not explicitly defined in the configuration |
| app | [Celery](../base/celery.md?sid=celery_app_base_celery) = None | The 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
| Name | Type | Description |
|---|---|---|
| options | dict | The initial execution options provided by the caller |
| name | string | The registered name of the task being routed |
| args | tuple = () | Positional arguments intended for the task execution |
| kwargs | dict = None | Keyword arguments intended for the task execution |
| task_type | Any = None | The task class or type object associated with the task name |
Returns
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| route | `string | dict` |
Returns
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| name | string | The name of the task to look up in the routing table |
| args | tuple = None | Positional arguments of the task used for content-based routing |
| kwargs | dict = None | Keyword arguments of the task used for content-based routing |
| options | dict = None | Current execution options that may influence the routing decision |
| task_type | Any = None | The task class or type object being routed |
Returns
| Type | Description |
|---|---|
| `dict | null` |
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
| Name | Type | Description |
|---|---|---|
| router | `callable | object` |
| task | string | The name of the task being routed |
| args | tuple | The positional arguments for the task |
| kwargs | dict | The keyword arguments for the task |
| options | dict | The task execution options |
| task_type | Any | The task class or type object |
Returns
| Type | Description |
|---|---|
| `dict | null` |