Eduardo Alfonso-Sierra
02/01/2024, 11:39 AMMarvin
02/01/2024, 11:40 AMpython
from prefect import task, Flow
common_task_config = {
'max_retries': 3,
'retry_delay': timedelta(minutes=5),
'timeout': 600,
}
2. Create Custom Tasks with Common Options:
Use the configuration object when creating tasks to apply the common settings.
python
@task(**common_task_config)
def task_1():
# Your task 1 implementation
pass
@task(**common_task_config)
def task_2():
# Your task 2 implementation
pass
3. Using a Base Task Class for Common Configuration:
Alternatively, you can define a base task class that inherits from Task
and apply the configuration there.
python
from prefect import Task
class MyBaseTask(Task):
def __init__(self, **kwargs):
super().__init__(**kwargs,
max_retries=3,
retry_delay=timedelta(minutes=5),
timeout=600)
class CustomTask1(MyBaseTask):
def run(self):
# Your Custom Task 1 logic
pass
class CustomTask2(MyBaseTask):
def run(self):
# Your Custom Task 2 logic
pass
4. Instantiate and Use in a Flow:
Use these custom task classes or task decorators in your flow.
python
with Flow("My Customized Tasks Flow") as flow:
result1 = task_1() # task_1 inherits common settings from common_task_config
result2 = task_2() # task_2 also inherits common settings
# or using your Custom Task class
result3 = CustomTask1()()
result4 = CustomTask2()()
5. Execution:
Finally, you run your flow and all tasks within it will adopt the common configurations you set.
python
flow.run()
These examples should give you the basic idea of how to customize many tasks with a common set of options. Remember, in the grand scheme of things, we're all just trying to find patterns in chaos. But in your case, it might actually help manage your tasks in Prefect.