Fugo Takefusa
08/04/2025, 11:27 PMMarvin
08/04/2025, 11:28 PMMarvin
08/04/2025, 11:28 PMplaintext
your_project/
├── shared_utils/
│ ├── __init__.py
│ ├── common_functions.py
│ └── helpers.py
├── flows/
│ ├── flow1.py
│ └── flow2.py
└── setup.py
2. Install Your Package
Create a `setup.py`:
python
from setuptools import setup, find_packages
setup(
name="your-shared-utils",
version="0.1.0",
packages=find_packages(),
install_requires=[
"prefect>=3.0.0"
# other dependencies
]
)
3. Use in Your Flows
Example of how to use shared code:
python
# shared_utils/common_functions.py
from prefect import task
@task
def shared_task():
return "I'm a shared task!"
# flows/flow1.py
from prefect import flow
from shared_utils.common_functions import shared_task
@flow
def my_flow():
result = shared_task()
return result
4. Deployment Options
There are several ways to ensure your shared code is available in deployments:
a. Git Repository Approach (Recommended):
python
from prefect import flow
from prefect.deployments import Deployment
@flow
def my_flow():
...
if __name__ == "__main__":
flow.from_source(
source="<https://github.com/your-org/your-repo>",
entrypoint="flows/flow1.py"
).deploy(
name="my-deployment",
work_pool_name="my-pool"
)
b. Requirements File Approach:
Create a requirements.txt
that includes your package:
plaintext
git+<https://github.com/your-org/your-shared-utils.git>
Best Practices:
1. Use Absolute Imports: Always use absolute imports rather than relative imports to maintain clarity and prevent issues.
2. Version Control: Keep your shared code in version control and use specific versions in your dependencies.
3. Documentation: Document your shared functions well, especially if they'll be used across teams.
4. Testing: Include tests for your shared code to ensure reliability across different deployments.
5. Dependencies: Be explicit about dependencies in your shared package's setup.py
or requirements.txt
.
Important Notes:
- When using Git-based deployments, make sure your shared code repository is accessible to your Prefect workers.
- Consider using semantic versioning for your shared package to manage updates and compatibility.
- If your shared code changes frequently, you might want to use branch or commit references in your requirements.
Would you like me to elaborate on any of these approaches or provide more specific examples for your use case?Fugo Takefusa
08/04/2025, 11:32 PMFugo Takefusa
08/04/2025, 11:32 PMMarvin
08/04/2025, 11:32 PMMarvin
08/04/2025, 11:33 PMplaintext
your_project/
├── shared_utils/
│ ├── __init__.py
│ ├── common_functions.py
│ └── helpers.py
├── flows/
│ ├── flow1.py
│ └── flow2.py
├── setup.py
└── prefect.yaml
1. Package Setup
python
# setup.py
from setuptools import setup, find_packages
setup(
name="your-shared-utils",
version="0.1.0",
packages=find_packages(),
install_requires=[
"prefect>=3.0.0"
# other dependencies
]
)
2. Example Shared Code
python
# shared_utils/common_functions.py
from prefect import task
@task
def shared_task():
return "I'm a shared task!"
3. Using in Flows
python
# flows/flow1.py
from prefect import flow
from shared_utils.common_functions import shared_task
@flow
def my_flow():
result = shared_task()
return result
### Part 2: Managing Updates
Important: Changes to your package do NOT automatically propagate to existing deployments. Here's what you need to know:
1. Package Versioning
When a deployment runs, it uses:
- The package version specified in the deployment configuration
- The latest version from the specified Git branch/commit if using Git-based deployments
- The version installed in the worker's environment if using a local package
2. Update Strategies
a. Git-based deployments (Recommended):
python
# deployment.py
from prefect import flow
@flow
def my_flow():
...
if __name__ == "__main__":
flow.from_source(
source="<https://github.com/your-org/your-repo>",
entrypoint="flows/flow1.py",
reference="main" # specify branch or commit
).deploy(
name="my-deployment",
work_pool_name="my-pool"
)
b. Using prefect.yaml:
yaml
# prefect.yaml
deployments:
- name: my-deployment
version: null # will auto-increment
tags: []
description: null
schedule: {}
flow_name: my_flow
entrypoint: flows/flow1.py:my_flow
parameters: {}
work_pool:
name: my-pool
work_queue_name: null
dependencies:
- git+<https://github.com/your-org/your-shared-utils.git@v0.1.0>
3. When You Make Changes
YES, you need to redeploy deployments when you want them to use the new version of your package. Here's what to do:
a. For Git-based deployments:
- Update your code and push to Git
- Redeploy each deployment that should use the new version:
bash
prefect deploy
b. For package-based deployments:
- Update your package version in setup.py
- Rebuild and republish your package
- Update the package version in your deployment configuration
- Redeploy each deployment
Best Practices for Managing Updates:
1. Version Control:
- Use semantic versioning for your package
- Tag releases in Git
- Specify exact versions in deployment dependencies
2. Deployment Organization:
- Group related deployments in the same prefect.yaml
- Use consistent naming conventions
- Document which deployments use which shared packages
3. Update Automation:
python
# update_deployments.py
from prefect.deployments import Deployment
from prefect.client import get_client
async def update_all_deployments():
async with get_client() as client:
deployments = await client.read_deployments()
for deployment in deployments:
if "your-shared-utils" in deployment.dependencies:
# Update deployment
await Deployment.load(f"{deployment.name}/{deployment.flow_name}")
await deployment.apply()
4. Testing Updates:
- Test changes in a staging environment first
- Consider maintaining multiple versions if backwards compatibility is needed
- Use deployment tags to identify which deployments use shared code
Remember:Marvin
08/04/2025, 11:33 PM