Stephen Lloyd

    4 months ago
    I have a mapped task that will make an api call. What is the best way to count successful api responses? I could check the response message in the task and return an int along with the response. It seems like
    will be a list of the responses and I could iterate over it in a subsequent task, but I’m hoping there is a more efficient method. Can I force a task to fail if the api indicates a failed call? If i do, is there a way to count those?
    def make_post(creds, task):
       response = requests.request('POST', ...)
       return response
    with Flow(stuff, stuff):
       send_tasks = make_post.map(creds=unmapped(creds), task=data)
    Kevin Kho

    Kevin Kho

    4 months ago
    So you can make a task fail with
    raise FAIL
    inside it where
    is the Prefect signal. And then you can have a reduce task
    def reduce(x: List):
        total = len(x)
        fail = len[1 for _ in x if isinstance(_, BaseException)]
        success = total - fail
    because the FAIL is an Exception so you can look for it like that.