A first look at Django's new background tasks

https://news.ycombinator.com/rss Hits: 16
Summary

Django 6.0 introduces a built-in background tasks framework in django.tasks. But don't expect to phase out Celery, Huey or other preferred solutions just yet. The release notes are quite clear on this: Django handles task creation and queuing, but does not provide a worker mechanism to run tasks. Execution must be managed by external infrastructure, such as a separate process or service. The main purpose of the new django.tasks module is to provide a common API for task queues implementations. Jake Howard is the driving force behind this enhancement. Check out the introduction on the Django forum. His reference implementation, and simultaneously a backport for earlier versions of Django, is available as django-tasks on GitHub. But let's ignore that and play with the more minimal version included in Django 6.0 instead. By creating our very own backend and worker. Our project: notifications We're going to create an app to send notifications to phones and other devices using ntfy.sh. (I'm a fan!) If you prefer to dive into the code yourself, check out the final version of the project on GitHub. All that's required to send a notification to your phone using nfty is: Register for an account Create a topic. Install the app for your phone and log in. Send HTTP requests to https://ntfy.sh/<yourtopic> The free version only provides public topics and messages. Meaning anyone can see the stuff you're sending if they subscribe to the topic. For our purpose we can simply create a topic with a randomized name, like a UUID. The project's settings expect the URL from step 4 to be supplied as an environment variable. For example: NTFY_URL=https://ntfy.sh/062519693d9c4913826f0a39aeea8a4c Here's our function that does the heavy lifting: import httpx from django.conf import settings def send_notification(message: str, title: str | None): # Pass the title if specified. headers = {"title": title} if title else {} httpx.post( settings.NTFY_URL, content=message, headers=headers, ) Really. ...

First seen: 2025-11-28 22:42

Last seen: 2025-11-29 13:44