As developers, we're tasked with writing code, with the intention of running it. In most cases, we want to run said code for as long as possible - either until completion or until something external restarts us. But in some cases, we want to run until another part of our program tells us not to.
When defining background reusable tasks, you don't want them to run forever. If the task takes too long, for whatever reason, you want the task to stop. For sockets and other native APIs, there are native timeouts. But what about entire blocks of code? Or CPU-intensive operations? How can you (or a background worker library) stop them, cleanly and reliably, from within Python, all without any prior knowledge of how the background task is written or executed?
Well, there are quite a few ways to do it. Some elegant, some not so much...
Similar content
Unleashing Tailscale with Public and Private Services
- 2025-07-03
- 20 minutes
- Slides
- Tailscale London Meetup
A talk about how I use Tailscale (well, technically Headscale) and why. Tailscale's promise is making networking easier, which I hate. But sometimes it's necessary to achieve something cool (building Funnel myself), and something not recommended (Private Funnel). The talk wasn't recorded, but my slides are available. However, I'd recommend reading the related post, instead.
Bringing Background Workers to Django
- 2024-09-10
- 20 minutes
- Slides
- Django London
This talk was almost identical to the one I gave at Djangocon Europe 2024. Django London isn't recorded, but the slides are linked. If you want to watch the talk, I recommend watching the Djangocon version. There's a lot more to a web application than just the request/response cycle. Running tasks in the background, whether on a trigger or schedule unlocks a lot of extra functionality, performance and…

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.