<@ULVA73B9P> I have a task which is calls an api t...
# ask-marvin
r
@Marvin I have a task which is calls an api to get a computation task status. Its called every 5 seconds until the status is "succeeded". The task's cache prevent me from this to be working. How can I disable the cache for that specific prefect task (I'm using prefect 3)
m
thought for 69.5 seconds
For your specific case where you're polling an API status every 5 seconds, you definitely want to disable caching since you need fresh results each time. Here's how you can disable caching for your task in Prefect 3.x:
Copy code
python
from prefect import task
from prefect.tasks import NO_CACHE

@task(cache_policy=NO_CACHE)
def check_computation_status():
    # Your API call code here
    return status
This solution is perfect for your use case because: 1. Setting
cache_policy=NO_CACHE
ensures the task runs every time it's called 2. It will allow your polling mechanism to actually check the API status every 5 seconds instead of returning cached results If you're currently using a task without any decorator parameters, just add the
cache_policy=NO_CACHE
parameter to your existing decorator. For reference, here's a complete example of how your polling logic might look:
Copy code
python
from prefect import task, flow
from prefect.tasks import NO_CACHE
import time

@task(cache_policy=NO_CACHE)
def check_computation_status():
    # Your API call here
    response = api.get_status()  # Replace with your actual API call
    return response.status

@flow
def monitor_computation():
    while True:
        status = check_computation_status()
        if status == "succeeded":
            break
        time.sleep(5)
    return "Computation completed successfully"
This will ensure that each status check actually calls the API instead of returning cached results.