Skip to content

Prefect2 How to set retry_on to task? #7063

Answered by tonal
tonal asked this question in Q&A
Discussion options

You must be logged in to vote
import httpx
from prefect import task
from httpx._exceptions import HTTPError

# Return False to prevent the orchestrator from retrying
def is_retriable(task, task_run, state):
    exc = state.result()
    if isinstance(exc, HTTPError):
        # Retry if not an authorization error
        return exc.args[0] != 401
    return True

@task(retry_condition_fn=is_retriable)
def my_api_call_task():
    r = httpx.get(...)
    r.raise_for_status()
    return r.json()

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@tonal
Comment options

Answer selected by tonal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants