Python CancelledError
Reading a successfully cancelled future raises CancelledError.
A simple example
CancelledError
- Start with A pending task is cancelled
- Do this Ask for its result
- Result Cancellation reported
Syntax
job.result()
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyCollect a cancelled print job
A student cancels a print job before the printer starts. When the dashboard later requests the result, it should show the cancellation as a distinct outcome.
from concurrent.futures import CancelledError, Future
job = Future()
job.cancel()
try:
print(job.result())
except CancelledError:
print("Print job cancelled")
Print job cancelled
The cancellation is visible before any result is collected. The dashboard can label the job accordingly.
Collect a finished poster
The print room records completion before the clerk calls for the result. Collect the result of the print job. A cancelled job should be removed from the collection queue.
Collect a withdrawn badge job
A class withdraws its badge job before printing begins. Collect the result of the print job. A cancelled job should be removed from the collection queue.
What to watch for
Cancellation can fail for an already running job. Here the future starts in the pending state.