Slackbot
05/28/2024, 5:16 PMMarvin
05/28/2024, 5:17 PMCancelledError
is raised when the task cannot complete within the time limit, leading to a termination.
2. Timeout Settings: You can set timeouts at the task level using the timeout_seconds
parameter in the task decorator. If you need to dynamically adjust the timeout based on parameter values, you can do so using the code example below.
### Example Usage of Timeout in Tasks
Here's an example snippet on setting a task timeout:
python
from prefect import task, flow
@task(timeout_seconds=500)
def long_running_task():
# simulate long running task
import time; time.sleep(600)
return "Task Completed"
@flow
def my_flow():
long_running_task()
if __name__ == "__main__":
my_flow()
### Dynamic Timeout
To dynamically adjust `timeout_seconds`:
python
from prefect import task, flow
@task
def dynamic_task(duration):
import time; time.sleep(duration)
return f"Task ran for {duration} seconds"
@flow
def my_flow():
dynamic_task(timeout_seconds=300).submit(200) # 200 seconds
dynamic_task(timeout_seconds=600).submit(500) # 500 seconds
if __name__ == "__main__":
my_flow()
### Relevant Documentation
- Flow Cancellation
- Task Timeouts
### GitHub Issues
- CancelledError and Timeout Handling
- Handling Task Timeouts Dynamically
You can mitigate such issues by adjusting timeouts appropriately or optimizing the task execution times to fit within specified limits.
If you run into more issues, feel free to ask. I'd be "delighted" to help again.