<@ULVA73B9P> can you give an example use case of a...
# ask-marvin
v
@Marvin can you give an example use case of a flow that returns something ?
m
thought for 46.6 seconds
Here's a practical example that demonstrates how a flow can return meaningful data that could be used by other flows or stored for later use:
Copy code
python
from prefect import flow

@flow
def fetch_stock_data(symbol: str) -> dict:
    """A flow that fetches and processes stock market data."""
    # Simulate fetching stock data
    stock_data = {
        "symbol": symbol,
        "price": 150.25,
        "volume": 1000000
    }
    
    # You could do more processing here
    return stock_data

@flow
def analyze_stocks():
    # Call the first flow and use its return value
    tesla_data = fetch_stock_data("TSLA")
    apple_data = fetch_stock_data("AAPL")
    
    # Process the returned data
    total_volume = tesla_data["volume"] + apple_data["volume"]
    return {
        "stocks_analyzed": [tesla_data["symbol"], apple_data["symbol"]],
        "total_volume": total_volume
    }

# Run the analysis
result = analyze_stocks()
print(result)  # Will print the dictionary with analysis results
In this example: 1. The
fetch_stock_data
flow returns a dictionary containing stock information 2. The
analyze_stocks
flow calls the first flow twice and uses its return values 3. The final result is a new dictionary containing aggregated information Return values are useful for: - Passing data between flows - Storing results in the Prefect backend for later reference - Chaining flows together where one flow's output becomes another's input - Creating reports or summaries of flow execution - Enabling conditional logic based on flow results Remember that return values from flows are automatically tracked and stored in Prefect's backend, making them available for inspection through the UI or API. This is particularly useful for debugging, monitoring, and analyzing your workflow results over time. The return value can be any Python object that can be serialized (dictionaries, lists, numbers, strings, etc.). However, if you're working with generator functions, they need to yield results rather than return them directly.
v
@Marvin Are task results stored in Prefect's backend too ?
m
thought for 62.5 seconds
No, by default task results are not stored in Prefect's backend. Here's how task result storage works: 1. Default Behavior: Task results are ephemeral and only kept in memory during flow execution. 2. Enabling Result Storage: You can enable result storage by: - Setting
persist_result=True
on individual tasks - Using a
result_storage
block to specify where results should be stored - Setting the environment variable
PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
Here's an example of configuring task result storage:
Copy code
python
from prefect import flow, task
from prefect.filesystems import LocalFileSystem

# Create a storage block for results
local_storage = LocalFileSystem(basepath="/path/to/results")

@task(persist_result=True, result_storage=local_storage)
def my_task():
    return "This result will be stored"

@flow
def my_flow():
    result = my_task()
    return result
You can also configure result storage at the flow level, which will apply to all tasks within that flow:
Copy code
python
@flow(result_storage=local_storage)
def my_flow():
    # All task results in this flow will use the specified storage
    ...
Key Points to Remember: - Result storage is optional and must be explicitly enabled - You need to configure a storage block (e.g., LocalFileSystem, S3, Azure, etc.) to persist results - Stored results can be useful for: - Debugging - Result caching - Sharing data between flow runs - Audit trails - Large results should be carefully considered as they can impact storage costs and performance If you're working with large datasets or sensitive information, you might want to be selective about which task results you persist and where you store them.
v
@Marvin OK so by default flow's return values are stored in Prefect's backend but not tasks' results which are aphemeral, is that correct ?
m
thought for 29.0 seconds
I need to correct my previous response. Actually, neither flow return values nor task results are stored in Prefect's backend by default. Both are ephemeral unless explicitly configured for persistence. Here's the accurate breakdown: Default Behavior: - Flow return values: Ephemeral (not stored) - Task results: Ephemeral (not stored) To persist either of them, you need to: 1. Enable persistence using
persist_result=True
2. Configure a
result_storage
block to specify where to store the results Example showing both flow and task persistence:
Copy code
python
from prefect import flow, task
from prefect.filesystems import LocalFileSystem

# Create a storage block
storage = LocalFileSystem(basepath="/path/to/storage")

@task(persist_result=True, result_storage=storage)
def my_task():
    return "Task result will be stored"

@flow(persist_result=True, result_storage=storage)
def my_flow():
    task_result = my_task()
    return "Flow result will be stored"
You can also enable persistence globally using the environment variable
PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
, which will affect both flows and tasks. Thank you for asking for clarification - it's important to be precise about these details!