Amith M
08/05/2025, 6:19 PMMarvin
08/05/2025, 6:19 PMMarvin
08/05/2025, 6:21 PMpython
from prefect import flow, task
from contextlib import contextmanager
@task
def setup_resources():
# Initialize your resources here
# e.g., database connections, API clients, etc.
resources = {
"db_connection": create_db_connection(),
"api_client": create_api_client()
}
return resources
@task
def cleanup_resources(resources):
# Cleanup your resources here
resources["db_connection"].close()
resources["api_client"].close()
@flow
def my_flow():
# Setup phase
resources = setup_resources()
try:
# Your main flow logic here
result = do_something_with_resources(resources)
finally:
# Cleanup phase
cleanup_resources(resources)
return result
2. Using Context Managers:
python
from prefect import flow
from contextlib import contextmanager
@contextmanager
def resource_lifecycle():
# Setup phase
resources = {
"db_connection": create_db_connection(),
"api_client": create_api_client()
}
try:
yield resources
finally:
# Cleanup phase
resources["db_connection"].close()
resources["api_client"].close()
@flow
def my_flow():
with resource_lifecycle() as resources:
# Your main flow logic here
result = do_something_with_resources(resources)
return result
3. Using Background Tasks:
python
from prefect import flow, task
from prefect.tasks import get_run_logger
@task(on_completion=[cleanup_task])
def main_task():
logger = get_run_logger()
<http://logger.info|logger.info>("Setting up resources...")
# Your task logic here
@task
def cleanup_task(task_future):
logger = get_run_logger()
<http://logger.info|logger.info>("Cleaning up resources...")
# Your cleanup logic here
@flow
def my_flow():
return main_task()
Key differences from FastAPI's lifespan:
1. In Prefect, the setup/teardown is more flow-specific rather than application-wide
2. Resources are typically managed at the task or flow level
3. You have more granular control over when setup and cleanup occurs
Best Practices:
1. Always use try/finally
or context managers to ensure cleanup runs even if there are errors
2. Consider using Prefect's built-in logging to track lifecycle events
3. Keep resource management close to where the resources are used
4. Use task retries and error handling for robust resource management
Remember that unlike FastAPI's lifespan which runs once for the entire application, in Prefect each flow run will execute its setup and teardown code. This means you should be mindful of resource usage and ensure proper cleanup after each flow run.
Would you like me to elaborate on any of these patterns or provide more specific examples for your use case?