title |
class |
highlighter |
transition |
mdc |
monaco |
themeConfig |
addons |
Empowering Django with Background Workers |
text-center |
shiki |
slide-left |
true |
false |
|
|
Empowering Django with Background Workers
Jake Howard
- Senior Systems Engineer @ Torchbox
- Core, Security & Performance teams @ Wagtail
- theorangeone.net
- @RealOrangeOne
- @RealOrangeOne
- @jake@theorangeone.net
layout: center
Django is a web framework
flowchart LR
U(User 🧑💻)
D[\Django/]
U---->|Request|D
D---->|Response|U
layout: full
Django isn't just for websites
flowchart TD
U[User 🧑💻]
D[\Django/]
DB[(Database)]
E>Email]
EA[External API]
V[[Video Transcoding]]
R[Reporting]
ML((Machine<br>Learning))
U<--->D
D---DB
D-..-E & EA & V & R & ML
layout: full
Background Workers!
flowchart TD
U[User 🧑💻]
D[\Django/]
E>Email]
EA[External API]
V[[Video Transcoding]]
R[Reporting]
ML((Machine<br>Learning))
B{{<strong>Background Processing</strong>}}
U<-->D
D-..-B
B---E & EA & V & R & ML
layout: section
Background Workers?
layout: fact
flowchart LR
D[\Django/]
S[(Queue Store)]
R1{Runner}
R2{Runner}
R3{Runner}
D<----->S<-....->R1 & R2 & R3
layout: section
When?
Does it take time?
layout: fact
Does it leave your infrastructure?
flowchart BT
D[\Django/]
subgraph Slow / Unreliable
E>Email]
EA[External API]
V[[Video Transcode]]
R[Reporting]
ML((Machine<br>Learning))
end
subgraph Fast & Reliable
DB[(Database)]
C[(Cache)]
end
D---DB & C
D-.-E & EA & V & R & ML
Specialized hardware?
Example:
Complex reporting
layout: section
Background Workers in
layout: cover
background: /celery.svg
Celery!
Others...
Celery
- arq
- Django DB Queue
- Django Lightweight Queue
- Django Too Simple Q
- Django-Q
- Django-Q2
- Dramatiq
- Huey
- RQ
- Taskiq
- ...
Example:
Email
layout: center
Sending an email
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
from wagtail.models import Page
for user in page.subscribers.iterator():
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
layout: center
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
import django_rq
from wagtail.models import Page
def send_email_to_user(page: Page, user: User):
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
for user in page.subscribers.iterator():
django_rq.enqueue(send_email_to_user, user)
layout: center
Using RQRQ Celery
```python
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
import django_rq
from wagtail.models import Page
def send_email_to_user(page: Page, user: User):
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
for user in page.subscribers.iterator():
django_rq.enqueue(send_email_to_user, user)
```
```python {all|7-9,20|all}
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
from wagtail.models import Page
from my_celery_config import app
@app.task
def send_email_to_user(page: Page, user: User):
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
for user in page.subscribers.iterator():
send_email_to_user.delay(user)
```
layout: image
image: /situation.png
backgroundSize: 50%
layout: image
image: /ridiculous.png
backgroundSize: 49%
layout: fact
Introducing*:
django.tasks
- API contract between library and application developers
- Swappable backends through
settings.py
- Built in backends:
- Django 5.2 🤞
layout: center
Using CeleryUsing django.tasks
```python
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
from wagtail.models import Page
from my_celery_config import app
@app.task
def send_email_to_user(page: Page, user: User):
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
for user in page.subscribers.iterator():
send_email_to_user.delay(user)
```
```python {all|7-9,20|all}
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
from wagtail.models import Page
from django.tasks import task
@task()
def send_email_to_user(page: Page, user: User):
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
for user in page.subscribers.iterator():
send_email_to_user.enqueue(user)
```
layout: center
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.template.loader import render_to_string
from wagtail.models import Page
for user in page.subscribers.iterator():
email_content = render_to_string("notification-email.html", {"user": user, "page": page})
send_mail(
subject=f"A change to {page.title} has been published",
message=email_content
from_email=None, # Use the default sender email
recipient_list=[user.email]
)
# settings.py
EMAIL_BACKEND = "django.core.mail.backends.tasks.SMTPEmailBackend"
layout: image-right
image: /soon.png
class: flex justify-center text-2xl flex-col
Q: Why something new?
Q: Why something built-in?
- Reduce barrier to entry
- Reduce cognitive load
- Reduce complexity for smaller projects
- Improve interoperability
- Use what's already there
- A common API
layout: center
transition: fade
{.h-32.mx-auto}
vs
{.h-36.mx-auto}
layout: center
{.h-32.mx-auto}
vs
{.max-h-36.mx-auto}
layout: section
Where are we now?
layout: image
image: /dep.png
layout: cover
pip install django-tasks
layout: section
Where will we be soon™?
layout: cover
background: /celery.svg
Is this the end?
Out of scope
- Completion / failed hooks
- Bulk queueing
- Automated task retrying
- Task runner API
- Unified observability
- Cron-based scheduling
- Task timeouts
- Swappable argument serialization
- ...
The future is bright
layout: section
What's next?
layout: end
END