skaaks
08/09/2024, 5:58 PMMarvin
08/09/2024, 5:58 PMpython
from prefect import flow
@flow(log_prints=True)
def my_workflow():
print("I can call any Python code here, including Prefect tasks or flows")
if __name__ == "__main__":
my_workflow() # Call it like a normal Python function
# Optionally serve it as a long-lived process
# my_workflow.serve("my-deployment-name")
(Code example source: https://raw.githubusercontent.com/zzstoatzz/prefect-code-examples/main/examples/flows/basic.py)
2. Usage Documentation:
In the Prefect deployment documentation, you can set up and deploy your flows while specifying code storage locations. Learn more from Deployment Guide and Storage Guide.
If no storage is explicitly configured, Prefect uses LocalFileSystem storage by default.
For more details, check out Prefect's Concepts of Storage.skaaks
08/09/2024, 6:01 PMMarvin
08/09/2024, 6:02 PMLocalFileSystem
storage if no other storage is explicitly configured.
Here's a simple code example of how to create and deploy a flow that assumes your code is already on the server:
python
from prefect import flow, deployment
from prefect.filesystems import LocalFileSystem
local_file_system = LocalFileSystem(basepath="/path/to/your/flow/code")
@flow
def my_local_flow():
print("Executing flow from local file system")
if __name__ == "__main__":
deployment.deploy(
flow=my_local_flow,
name="local-deployment",
storage=local_file_system
)
### Steps:
1. Replace "/path/to/your/flow/code"
with the actual path where your flow code resides.
2. Run this script on your server to deploy the flow using the local file system.
### Documentation:
You can read more about this setup in the Prefect documentation:
- Flow code storage options
- Quickstart
By following these instructions, you can ensure the Prefect deployment uses the local code stored on your server.skaaks
08/09/2024, 6:04 PMMarvin
08/09/2024, 6:04 PMprefect.deploy
function to deploy a local flow. Below is an example extracted from the documentation which demonstrates deploying both a local flow and a flow from a remote repository:
python
from prefect import deploy, flow
@flow(log_prints=True)
def local_flow():
print("I'm a flow!")
if __name__ == "__main__":
deploy(
local_flow.to_deployment(name="example-deploy-local-flow"),
# Example of a remote flow deployment for comparison
# flow.from_source(
# source="<https://github.com/org/repo.git>",
# entrypoint="flows.py:my_flow",
# ).to_deployment(
# name="example-deploy-remote-flow",
# ),
work_pool_name="my-work-pool",
image="my-registry/my-image:dev",
push=False, # Indicates that the code should not be pushed to any repository or remote storage
)
### Key Points:
1. Local Flow: The local_flow.to_deployment(name="example-deploy-local-flow")
line is where the local flow is specified for deployment.
2. work_pool_name: Indicates the work pool with which the deployment should be associated.
3. image: The Docker image to be used (if required).
4. push=False: Ensures that the code is not pushed to a remote repository or storage, keeping it local.
### Documentation:
Learn more from Prefect's Deployment Guide.
By following this example, you can ensure that your deployment uses the code that is already on the server without needing to pull it from a remote location.skaaks
08/09/2024, 6:05 PMMarvin
08/09/2024, 6:06 PMpython
from prefect import flow, deploy
from prefect.filesystems import LocalFileSystem
# Define the flow
@flow(log_prints=True)
def local_flow():
print("Executing flow from local file system")
# Define the storage location
local_file_system = LocalFileSystem(basepath="/path/to/your/flow/code")
if __name__ == "__main__":
# Deploy the flow using the LocalFileSystem
deploy(
flow=local_flow,
name="local-deployment",
storage=local_file_system,
work_pool_name="my-work-pool",
)
### Steps:
1. Replace "/path/to/your/flow/code"
with the actual path where your flow code resides.
2. Run this script on your server to deploy the flow using the local file system without Docker.
### Example Source:
Here is a simpler flow example without deployment; to illustrate the basic setup: Basic Flow Example.
This approach ensures your Prefect deployment uses the code stored locally on your server without involving Docker.Nate
08/09/2024, 6:09 PMNate
08/09/2024, 6:09 PMif __name__ == "__main__":
my_flow.from_source(
source=str((p := Path(__file__)).parent),
entrypoint=f"{p.name}:my_flow",
).deploy(
name="my-deployment",
parameters=dict(name="Marvin"),
work_pool_name="local",
)
skaaks
08/09/2024, 6:36 PM# build section allows you to manage and build docker images
build:
# push section allows you to manage if and how this project is uploaded to remote locations
push:
# pull section allows you to provide instructions for cloning this project in remote locations
pull:
# the deployments section allows you to provide configuration for deploying flows
deployments:
- name: nba_api_pipeline
version:
tags: []
description:
entrypoint: flows/nba_api/nba_api_pipeline.py:nba_api_pipeline
parameters: {
process_id
}
work_pool:
name: default_work_pool
work_queue_name: nba_api_pipeline
job_variables: {}
schedules: []
skaaks
08/09/2024, 6:37 PMskaaks
08/09/2024, 6:38 PMNate
08/09/2024, 6:39 PMprefect.yaml
- have you explored serve
and know that you don't want to use it? it sort of circumvents this problemskaaks
08/09/2024, 6:41 PMNate
08/09/2024, 6:42 PMNate
08/09/2024, 6:42 PMserve
ends up workingskaaks
08/09/2024, 6:43 PMNate
08/09/2024, 6:46 PMpull
step of your deployment. so totally possible, but with serve
you only have to run the file where it lives anyways, so there's no ambiguity / decoupling to worry about (which to be clear, lots of times work pools / workers are necessary and nice, but not always)skaaks
08/09/2024, 8:31 PMfrom flows.nba_api import nba_api_status_log_pipeline
nba_api_status_log_pipeline.nba_api_status_log_pipeline.serve(
name="nba_api_status_log_pipeline",
cron="*/5 * * * *"
)
Nate
08/09/2024, 8:33 PMnba_api_status_log_pipeline
directly instead of the module it lives in?Nate
08/09/2024, 8:33 PMNate
08/09/2024, 8:34 PMserve
completely circumvents the need for a worker. so, there shoulde be no worker involved if you go down the serve
routeskaaks
08/09/2024, 8:59 PMskaaks
08/09/2024, 9:03 PMskaaks
08/09/2024, 9:13 PMNate
08/09/2024, 9:16 PMskaaks
08/09/2024, 9:52 PM17:41:10.506 | INFO | prefect.flow_runs.runner - Runner 'nba_ngss_launcher_service' submitting flow run '27b8b291-887d-470a-95f6-c0b5b81ce7bf'
17:41:10.547 | INFO | prefect.flow_runs.runner - Opening process...
17:41:10.569 | INFO | prefect.flow_runs.runner - Completed submission of flow run '27b8b291-887d-470a-95f6-c0b5b81ce7bf'
'c:\Code\CEP' is not recognized as an internal or external command,
operable program or batch file.
17:41:10.596 | ERROR | prefect.flow_runs.runner - Process for flow run 'nice-gazelle' exited with status code: 1
17:41:10.633 | INFO | prefect.flow_runs.runner - Reported flow run '27b8b291-887d-470a-95f6-c0b5b81ce7bf' as crashed: Flow run process exited with non-zero status code 1.
To me, it looks as though it's having an issue with the space in the full flow file path and not using the full path to locate the fileNate
08/09/2024, 10:01 PM