Slackbot
01/25/2024, 7:43 PMMarvin
01/25/2024, 7:43 PMpython
from prefect import task, Flow
from prefect.schedules import Schedule
from prefect.schedules.clocks import IntervalClock
from datetime import timedelta, datetime
# Define a task that takes a parameter
@task
def parametrized_task(my_param):
print(f"Task received parameter: {my_param}")
# Define the schedule
schedule = Schedule(clocks=[IntervalClock(interval=timedelta(hours=1))])
# Function to generate dynamic parameter
def generate_dynamic_param():
# Here you could add any logic to generate the parameter dynamically
return {"my_param": "dynamic_value"}
# Create the flow
with Flow("Dynamic Parameter Flow", schedule=schedule) as flow:
# Schedule the task with the dynamic parameter
scheduled_task = parametrized_task(my_param=generate_dynamic_param())
# Run the flow (for example in a script or via CLI)
if __name__ == "__main__":
flow.run()
This example uses an IntervalClock
to schedule the flow to run every hour. You can customize the schedule and the function generate_dynamic_param
to generate parameters dynamically for each flow run based on your specific requirements. Please note that the specific implementations and features may differ depending on the version of Prefect and whether you are using Prefect 1.x or Prefect 2.x (Orion).
Ensure to check Prefect's official documentation or GitHub issues if you encounter any problems or require features specific to the Prefect version you are using.Marvin
01/25/2024, 7:47 PMpython
from prefect import task, Flow
# Define a task that takes a parameter
@task
def parametrized_task(my_param):
print(f"Task received parameter: {my_param}")
# Your list of arguments
params_list = ["param1", "param2", "param3"]
# Create the flow
with Flow("Flow for List of Arguments") as flow:
# Schedule the task for each argument in the list
for param in params_list:
parametrized_task(my_param=param)
# Run the flow
if __name__ == "__main__":
flow.run()
This code creates a flow that runs the parametrized_task
for each item in params_list
. When flow.run()
is called, it will execute parametrized_task
three times with "param1", "param2", and "param3" as arguments for my_param
.
Remember, this is a very simple example and might not handle the concurrency or the dependencies you might have in a complex workflow. However, it should provide a baseline for running tasks with different parameters in a Prefect flow.