Teach Django Tasks to speak Celery: Building a Celery backend for Django Tasks with MatĂas Bordese
Transcript
By MatĂas Bordese, Speaker 2, Speaker 3. From DjangoCon US 2026. Published October 26, 2026.
Watch the video Markdown version 3,450 words · auto-generated · en
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
MatĂas Bordese
Hey, I'm MatĂas. I'm happy to be here. A little bit nervous up here on stage, but let's see how it goes. The purpose of this talk is to talk about Django Tasks and the new framework that is available in Django since the 6.0 release, and particularly focused on how a backend works. So we will go through some internals of how a backend is defined and implemented. Even if Celery is mentioned in the title, there is no real previous knowledge needed of Celery because it was mostly an excuse to investigate and review the backend details, but it's not needed for the purpose of following the
talk. So yeah, I'm MatĂas. I work at Grafana. I'm part of the IRM team, where we implement a plugin for Grafana for dealing with alerts and pages. We have a plugin implemented as a backend using the Django REST framework for the API. And on the other end, we have some Celery services dealing with all the alert ingestion and the triggering of escalations on paging people. So we use a lot of Celery to handle all that. And that's where my context using Celery comes from. So I have this kind of love-hate relationship with Celery. I've been using Celery for a lot of years, but recently it's been mostly love, so
I'm happy to use LR when I can, but this is not the purpose of the code. So what are we going to talk about here? The main goal is to go through why we need Django Tasks as part of the framework. Also go through the task interface. So what's what you, as a user, will be dealing with to use Django Tasks. And then go through the different backend responsibilities and the implementation details that you will need to implement a backend specifically for some particular engine. And then a live demo, because we have learned that they always work. So let's go with another try. And yeah, finally, some conclusions around the content of the talk.
So let's go. The problem we are trying to solve. A lot of times, you need in a Django app to do work outside of the request-response cycle, like sending a welcome email, rebuilding an index, generating a report, or paging a user. So you don't want the user waiting on that. You want to trigger something that will be handled in the background and do the work. Usually, people go through different solutions for that, like a cron job in the most basic case, or some table in the database to handle tasks as a queue, or Celery or RQ, which are frameworks much more complex
that allow you to do that and much more. The problem with that is that your business code ends up importing and depending on this engine you choose as a backend for dealing with the background task. And that's where Django Tasks is important, or where the problem it tries to solve comes in. It basically breaks this coupling we have between our business code and the background tasks. So Django Tasks will provide you with an interface, an API for you to write the task and make it possible to send it to the worker, to the background worker. And on the other end, the people implementing the backend, who need not be the same people using the task framework,
will have a contract to follow, so you can choose which engine to use independently of your business goal. So that's the main goal of Django Tasks. If you haven't used Django Tasks before or you are not familiar with it, this slide summarizes how it works. You import a decorator, you decorate a function that you want to run as a background task, and then later you can call the enqueue method with the corresponding parameters, or even customize the parameters of the task itself using the using method before the enqueue. So it's a really simple API, and it's nice to use if you haven't yet tried it.
So, summarizing the model, you have the task, which is backend agnostic. You have a setting in the Django project settings that will define a backend that you want to use to run the task, and you have possibly a worker which will be picking up the tasks and running them on the other side. We are going to try to focus on the backend implementation details now. So the Django Tasks framework provides us with a base task class and a base task backend class, which is the main interface that we need to implement. It provides us with a few things already figured out for us, like all the validation and some wrappers, as well as importing the config
data from the project settings. But it's your responsibility to implement the enqueue method mainly, which is the method that will translate the framework task into the engine running the task in the backend. And also, you need to implement the get_result method. So a backend can provide different features, which are also signaled by those flags that you see there, like supports_defer, supports_async_task, supports_priority, or supports_get_result. So you don't really need to implement the whole thing. It will depend on the features you want to support in your backend. But the only method you must implement for sure is the enqueue
method. Along with all this configuration, you have these check and validate methods that will perform all the validation for you based on these flags that you set up. You can also extend the checks to force additional settings or some other requirements that your backend may have. For example, we will see later, Celery can work as a fire-and-forget-only task queue. So you don't necessarily need to have a result backend, but the task framework allows you to have a result. So more on that later. Then, yes, the framework will define the shape and the interfaces we need to implement, and we
need to implement the enqueue method, mainly. The Django Tasks framework in core Django provides two basic backends that you can take a look at because they are less than 100 lines, but they are really useful for production. You have the dummy backend and the immediate backend. The dummy backend will just store the metadata of the task without running anything, and the immediate backend is just when you call enqueue, you're really executing the task. So there is no async task or async work running anywhere. It's just the same process, in the same enqueue call, running your task. So before proceeding with
enqueue, we need to know the data we are managing in this process. When the enqueue method is called, you will get a task instance that has this interface. It basically keeps a reference to the function that you were calling as a task, and the metadata of the parameters of this call, like the priority, the queue, or the backend that will run the task. And as output, you need to build a task result. The task result is the instance that you will return in enqueue and that you can get later or refresh when you want to track the progress of a task, if your backend
supports getting results. So the main thing here is Django Tasks summarizes the status of a task in four statuses. So you need to map whatever your backend supports. And you need to be able to return the exception following the task error interface that we went through, but it's keeping the traceback mainly. So, what else? We have the interface for the backend, which is based on the base task class, and the task in and the task out, which are the task instance and the task result. But the other thing that the backend should implement is the signals. These signals are emitted at different points in the task
life. Particularly, we have three signals provided by the backend. One is task enqueued, which is triggered when Django Tasks sends the task to the backend, and then we have the started and finished signals, which will be emitted on the worker side when the task is indeed started or finished, either with a success or fail status at the end. Good. So if we summarize all this we have been reviewing, we identify five responsibilities a backend
should implement or should address, which are: declare the capabilities that your backend will support; identify the function that will be run as a task; make it possible to enqueue the task, which is translating the data we get from the framework into the engine representation; execute the task itself, which basically is the main thing running the task, which can be a separate process or anything you want; and report, which is getting the result information back from the engine into the task framework. To go through all these responsibilities, I will iterate on three different existing backends.
I will take a look at the immediate backend, the database backend, and a Celery-based backend. That's why here I have a short slide summarizing how Celery works. The main idea is you have a queue, which is a message broker like Rabbit or Redis, where you put messages that the worker, which is a long-running process, will consume from the task queue and run the task. And optionally, a result backend, which can store the information and the progress of the task. So that's Celery in just a few words. It's a little bit more complex than that, but for the purpose of
integrating or adapting it to the task framework, we don't need much more than that. Here we have just a summary table comparing the concepts on the Django Tasks and Celery sides, which we can skip for now. But the idea is we have a mapping, and we need to adapt or make decisions on how we translate one thing to the other to keep the framework requirements satisfied. So the idea here is to go through all these responsibilities we define for each of these backends. The backends are these three: the immediate backend, which is built into Django.
There isn't a worker running for this backend; it's just the execution of the function inline. Then the database worker, which is a third-party package, so you need to pip install it. This package is from the author of the original Django Tasks package. Then we have this final package that I worked with in the context of this talk, which is a backend based on Celery. And that's why it has this name, because it's in the family but it's not Celery, and also because Django Tasks Celery was already taken on PyPI, so naming things is hard. You can take a look at the other backend also. Here we are.
So, what your engine supports. We mentioned that we have these flags that you should enable for each backend depending on the features you want to support. Here we can see that the immediate backend doesn't support the defer feature, for example, and the framework will validate this and will raise an exception identifying the issue if you try to do something the backend doesn't support. All the other two will support the whole feature set: defer, async task, priority, and get result. Then you need to be able to reference the function that you were
sending as a task. That's because the framework requires you to build a task or a task result that still references the original function, so you need to be able to reverse this. In particular, the immediate backend is not an issue because you are running inline and you have the object in there. The database backend will keep information in the database to allow the rebuilding of this. And in Celery we have this task registry that allows us to get the import path of the task, and we can build it from there. So the most important method that you need to implement is enqueue. Enqueue will translate
Django Tasks fits into whatever your engine wants. So it will hand off the work to the engine, trigger the signal that the task was enqueued, and get a task result that you'll be able to query later for updates or refresh. In the immediate backend, the enqueue method basically calls the function. For the database backend, we add a row to the database. And for the Celery backend, we use Celery internals to enqueue the task and do the translation in the backend implementation to map the right values to the...
Then we have the execute. The execute is where most engines differ, because it's basically what makes it possible to consume the task wherever it was queued and run it. For the immediate backend, there is no worker; the task is run directly. For the database worker, we have the particularity that the same package implements the polling worker, which consumes the tasks from the database and updates the database with the information there. And for the Celery backend, we're using the Celery worker itself.
For this final backend, on one hand, we have the backend mapping and translating all the Django Tasks implementation and representation to Celery. On the other hand, we have the Celery worker consuming tasks, as usual, from RabbitMQ or Redis message brokers. And finally, you need to be able to rebuild all the information you've been processing into a task result. In the case of the immediate backend, there's no way to do that because once the task is finished, you don't have any more reference to the metadata or the result of the task. The database will read from the database, and for Celery, we...
If we set up a backend, we can get the information from the result backend. And there are also a few conversions and translations needed to make sure that we have all the information that the task framework expects. This is all really high-level because time doesn't allow us to get into more details, but the idea you should take from this is: you write code in Django without importing anything external to Django core itself. You can define tasks, and you can configure your backend independently of your code and switch the backend transparently. So your backend...
It's not the most important thing for the purpose of writing the code. So, it's live demo time. Let's see. The idea here is I have a simple project in which I trigger an escalation after receiving an alert, and it will try to notify some people—an on-call person, the team, or the manager—with different delays. So it's a task queue that then queues another three tasks using the run_after parameter. Let's see if things work.
This is the code. I don't know if it's visible enough, but it's almost the same code you were looking at in the slide. And there are my project settings. Right now, it's configured to use the immediate backend, which won't necessarily work because it doesn't support the defer feature. Let's see. So here, if I trigger an alert, I'll see that nothing happens because the backend doesn't support the run_after parameter, right? So we could, even if it supports...
If we trigger a task that the immediate backend supports, we'll see that the task is executed immediately without any delay, any background work. So let's quickly switch. You'll see that I'm just changing a setting. I have the database backend in this case, for which I need to run the worker that will consume from the queue in the database. In this case, if I trigger a new alert, I should see this progressing here. There you go. And here...
The task is running in the logs of the DB worker. So this is consuming from the DB. Similarly, we can now switch to the Celery backend, again changing only one setting in the project, and I can run the Celery worker. Here you'll see that I'm mapping the task name with a Django task prefix. I'm also running the worker with a Django Tasks Celery app that I generate from your task configuration on the Django side.
So I'm handling all this for you. You just need to run the worker. And if we trigger an alert now, oops, it is progressing, and we can see the logs here, also paging, paging. As you can see, really nice. You only change one setting, and you can switch your backend depending on your needs. I almost ran out of time, so this is what I was talking about. The important thing is this: you didn't change the task, the view, the models, or anything. Just one line of settings, and you can switch the backend transparently. Your business code doesn't know anything about the backend...
...you're using, and you can write your code without depending inherently on any specific library or package. And that's really nice. Even if the Django Tasks framework is not super powerful, because you have a lot more features available in Celery or RQ, for example, you can still do async work and don't force people to use any specific backend. You can just distribute your package depending on the task framework, and people can use the backend they feel better with. So what do we have here? Django Tasks is an abstraction. It's not trying to replace Celery or RQ or anything. It's a simpler and smaller backend...
...framework. Right now, there isn't a production-ready backend in Django core itself, so you still need to install something external to use it. You have the option to implement your own backend. It's just implementing and doing the translation between your package and the Django Tasks framework in both directions. And yeah, you're not locked in. You can switch your backends if you write code following the task API. So that's it. If you're planning on using background tasks, maybe consider switching to the task framework and giving it a chance.
You can then change the backend as you need or as your possibilities allow. So here is the repo implementing the Celery backend, the task documentation, and the Celery documentation. And yeah, we're almost out of time, so I don't know if we have time for questions, but you can ask me questions outside.
Speaker 2
Thank you. It's time for one question if somebody wants to ask one. Otherwise, we'll move on. I'll take it. Oops.
Speaker 3
So when you were writing this, how much did you find yourself having to dig into Celery internals, specifically with how it interacts with AMQP?
MatĂas Bordese
Yeah, there are some limitations with the interfaces exposed by Celery and the requirements from the task framework, too. Particularly, I have to deal with some Celery internals to keep some additional metadata when I enqueue a task so I can retrieve the task later. So I have some side channel storing some data. And then, yes, for priorities, not all backends behave the same. For example, Rabbit has some priority, but Redis is more flexible, let's say. So yeah, there are a lot of compromises and decisions that you need to...
...make in the process. Still, I mean, Django Tasks—the idea is to be a simpler framework around background tasks, and in my opinion, at least, it satisfies a lot of scenarios for people that weren't considered before. So it's really nice to have it part of Django right now.