PyCon UK 2025

Halt and Catch Fire: Forcefully Stopping Python
2025-09-20 , Main stage

Writing code which runs is easy. Writing code which stops (cleanly) is hard. Writing code which cleanly stop someone else's code? Harder still.


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...


What level of experience do you expect from your audience for this session?:

Intermediate

Social:
- Mastodon: @jake@theorangeone.net
- Bluesky: @theorangeone.net