Handling Cancellation
Handle user cancellation for long-running tasks.
The on_cancel Hook
Define an async on_cancel method in your App class. This method is called when the supervisor receives a cancellation signal.
python
class App(BaseApp):
async def setup(self, config):
self.cancel_flag = False
async def on_cancel(self):
"""Called when user cancels the task"""
print("Cancellation requested...")
self.cancel_flag = True
# Return True to confirm you received the signal
return True
async def run(self, input_data):
self.cancel_flag = False
# Long running loop
for i in range(100):
if self.cancel_flag:
print("Stopping work...")
# Clean up resources if needed
break
await self.heavy_computation(i)
Best Practices
- •Check Frequently: In loops, check your cancellation flag at the start of every iteration.
- •Clean Up: Close database connections, delete temporary files, or free GPU memory before exiting.
- •Return Quickly: The
on_cancelhandler should be fast. Do not block execution there; just set a flag or signal an event. - •Force Kill: If an app does not respond to
on_cancelwithin a timeout period (default 30s), it will be forcefully terminated (SIGKILL).