[Solved]-Celery v4 not routing tasks as expected


routing_key as a class attribute is deprecated in v4.

You can supply it to a task on invocation as
a kwarg to apply_async or you can manually configure routing task types to queues in celery config.
See the docs

The Task class at celery.task.base.Task is deprecated for celery.app.task.Task.

This is a design change where the new Task class prefers to bind configuration when the task is used as opposed to at instantiation time.
See this comment

Looking through the source you can see routing_key is set when the task is bound using _get_exec_options. Here the options are set using an instance (self) not the class.

I’ve gathered this by looking at the celery source code but if there’s no mention of deprecation in the docs I guess they’re out of date.

Perhaps filling an issue on their github and using one of the options provided in the new paradigm is the best option going forward.

Also there’s in the doc that tasks routing_key will be read from task metadata, so class based tasks can’t have this metadata anymore?

Correct, the metadata isn’t on the class object anymore. It is set via config or lazily via a task instance when called.

The config file should be the goto for static routing while setting on task call should be used for overriding the configured default.

Leave a comment