Hi all and <@ULVA73B9P>, I would like to run a tas...
# ask-community
p
Hi all and @Marvin, I would like to run a task with a runtime determined timeout and have that task get cancelled if a TimeoutException is raised but all the answers I have gotten from @Marvin for prefect 3.x don’t work and the answer when using prefect 2.x is that it is not possible. I wanted to check if anyone else has tried this or can point me to documentation on how I might do this. Another option that I was exploring based on the documentation is making use of two async tasks that run concurrently and if the secondary task which just runs an async.sleep representing the runtime determined timeout ends then cancel the other job by making use of
async.wait(…, return_when=asyncio.FIRST_COMPLETED)
but again, I would need to cancel the pending tasks. Prefect team, is there no way of cancelling a task?
m
In Prefect 3.x, handling task timeouts and cancellations can be a bit tricky, but it is possible. Here's what I've gathered: 1. Task Cancellation: Prefect supports task cancellation, but there are conditions. The infrastructure running the task must support cancellation. If the infrastructure can't be found or has already been terminated, the task will be marked as canceled, but actual cancellation might not be enforced. More details can be found here. 2. Timeout Handling: The
TaskRunTimeoutError
is raised when a task exceeds its timeout. You can handle this exception within your task to perform any cleanup or logging before the task is marked as failed. The
task_engine.py
source code provides more insights into handling timeouts and task states. You can explore it here. 3. Concurrency with Async Tasks: Your idea of using
asyncio.wait
with
asyncio.FIRST_COMPLETED
is viable. You'd handle the cancellation logic within the async task that completes first. Prefect tasks can be defined as async functions, allowing you to integrate this logic directly into the task. For more detailed guidance, especially on configuring your infrastructure to support these features, you might need to refer to the infrastructure-specific documentation or community forums. If you have any specific issues or errors, feel free to share them, and I can assist further.