Adeel Shakir
09/01/2024, 2:31 PMMarvin
09/01/2024, 2:31 PMAdeel Shakir
09/01/2024, 2:32 PMMarvin
09/01/2024, 2:32 PMImran Nooraddin
09/02/2024, 11:54 AM@task #EDIT THIS TO INCLUDE RATE LIMITS
def add(x: int, y: int) -> int:
return x + y
@task # EDIT THIS TO INCLUDE RATE LIMITS
def multiply(x: int, y: int) -> int:
return x * y
@flow
def add_and_multiply(x:int, y:int):
sum = add(x, y)
product = multiply(x, y)
return sum, product
Marvin
09/02/2024, 11:54 AMpython
@task(tags=["rate_limit"]) # Add your desired tags here
def add(x: int, y: int) -> int:
return x + y
@task(tags=["rate_limit"]) # Add your desired tags here
def multiply(x: int, y: int) -> int:
return x * y
@flow
def add_and_multiply(x:int, y:int):
sum = add(x, y)
product = multiply(x, y)
return sum, product
As per your question about file structure, yes, it is common practice to include the tags in the same file where the tasks are defined. This makes sure that everything related to your specific task is centralized and easier to manage. Spread the joy of configuration and tagging.Imran Nooraddin
09/02/2024, 11:55 AMMarvin
09/02/2024, 11:55 AMrate_limit
parameter in the @task
decorator. Here's a revised version of your code with rate limits included for each task:
python
from prefect import task, flow
@task(tags=["rate_limit"], rate_limit=1) # Adjust the rate limit value as needed
def add(x: int, y: int) -> int:
return x + y
@task(tags=["rate_limit"], rate_limit=1) # Adjust the rate limit value as needed
def multiply(x: int, y: int) -> int:
return x * y
@flow
def add_and_multiply(x:int, y:int):
sum = add(x, y)
product = multiply(x, y)
return sum, product
Replace the rate_limit=1
with the actual rate limit you prefer. This limits the tasks to one execution per second unless specified otherwise.
Isn't it just exhilarating to control the rate at which things don't crash?Imran Nooraddin
09/02/2024, 11:58 AMMarvin
09/02/2024, 11:58 AMpython
from prefect import task, flow
@task(tags=["rate_limit"])
def add(x: int, y: int) -> int:
return x + y
@task(tags=["rate_limit"])
def multiply(x: int, y: int) -> int:
return x * y
@flow
def add_and_multiply(x:int, y:int):
sum = add(x, y)
product = multiply(x, y)
return sum, product
To implement rate-limiting with tags, you generally have to configure it in your deployment's settings. Here's how you might outline it in a deployment YAML file:
yaml
version: 2
flows:
- name: add_and_multiply
parameters:
x: 10
y: 5
agents:
- agent:
name: my-agent
labels: [default]
infrastructure:
- name: docker-container
image: prefecthq/prefect:2.14.0
task_defaults:
- tags: ["rate_limit"]
max_retries: 3
retry_delay: 10
concurrency:
- tag: "rate_limit"
concurrency: 1
You will have to replace the example values with what's meaningful to your setup like the image tag, task defaults, and concurrency values. This convoluted approach should get you that rate limiting you so desperately need.
What a marvel of simple configuration achieving something that could have been straightforward.