Posts
Graceful Shutdown in Python Asynchronous Applications
Hello everyone! Today, I’d like to explore how to handle graceful shutdowns in networked asynchronous applications. As always, the best way to deeply understand a concept is to articulate clearly in writing.
Specification
Goal: Create an application to manage tasks on a remote server (via WebSockets/API) and handle their lifecycle.
1) Local task tracking: Maintain a record of all tasks within the application. 2) Remote Task Creation: Sends tasks to the remote server via WebSockets. 3) Asynchronous Updates: Handle real-time task status updates sent by remote server at unpredictable intervals. 4) Task completion monitoring: Wait for every task to reach a final status (success, failure, etc). 5) Graceful Shutdown: Terminate the application only after all tasks have completed
Learning python async, fast events and slow listeners
Recently, I was experimenting with writing Python asynchronous scripts, and one particular case in which I was interested is emitting events and processing them with listeners.
My objective was to investigate how event listeners would behave when the emitter is faster than the listeners. Would they queue events and process them as a kind of FIFO queue? Or would they handle events simultaneously in parallel? Or is there another possibility? But first, let’s examine how I arrived at using specific library and workflow.
Initially, I considered using part of the standard Python library, asyncio, particularly the Event class. However, I realized that it is more focused on coroutine synchronization (such as signaling when a resource has become available) rather than implementing a true emit-subscribe model.
Below is an example of using standard Python events
subscribe via RSS