Emmanuel Rambeau
02/24/2024, 2:59 PMMarvin
02/24/2024, 3:09 PMpython
import time
from prefect import flow, task
@task
def sleep(t: int):
print(f"Going to sleep for {t} seconds...")
time.sleep(t)
print("...and I'm awake!")
@flow(log_prints=True)
def my_flow():
sleep(3)
future = sleep.submit(2)
future.wait()
sleep.map([5] * 10)
if __name__ == "__main__":
my_flow()
In this script:
- We have a simple task sleep
that takes a number t
which represents the number of seconds to sleep.
- We have a flow my_flow
which calls the sleep
task in different ways (synchronously, asynchronously with submit
, and concurrently with map
).
- By executing this script, the flow my_flow
will be run, which includes running the sleep
task.
To actually deploy this flow to the remote server, make sure you:
1. Authenticate with the remote Prefect server from your development machine.
2. Save the flow as a file in your development machine.
3. Deploy the flow using the Prefect CLI or UI and point it to your remote server.
Once these steps are completed, the remote server should pick up the flow and start executing it according to your configuration.
Please adjust the example code and the deployment procedure to fit into your environment configuration and deployment strategies. If you need more specific instructions on any of these steps or need assistance with configuring your remote server, please let me know.
For more information, use this code example URL:
Prefect Submitting Tasks Code ExampleEmmanuel Rambeau
02/24/2024, 3:18 PMMarvin
02/24/2024, 3:19 PMpython
from prefect import Client
# Set Prefect API URL
prefect_api_url = '<https://url-to-your-prefect-server>'
# Set the credentials (these could also be picked up from environment variables)
username = 'your_username'
password = 'your_password'
# Create an authorization token
auth_token = f'{username}:{password}'
# Initialize the Prefect Client
client = Client(api_server=prefect_api_url)
# Set the Authorization header on the client
client._session.headers.update({
"Authorization": f"Basic {auth_token}"
})
# Now you can interact with the Prefect API
# For example: Creating a project, registering a flow, etc.
Please ensure that you encode the auth_token
with Base64 encoding if you are passing basic credentials in a header. In production, it is always advisable not to hard-code credentials, but to use secrets management or environment variables.
This example doesn't come from a direct link, but illustrates the general idea. Depending on the authentication method (basic auth, token auth, etc.), you might need to adjust the example accordingly.
Note that passing credentials in code or via environment variables should be done securely and in accordance with best practices for secrets management.
For more information and examples of deploying flows and interacting with the server, use the example code URL related to the deployment and serving of flows:
Prefect Deployment Serving ExampleEmmanuel Rambeau
02/24/2024, 3:22 PMMarvin
02/24/2024, 3:33 PMrequests
to provide HTTP Basic Authentication:
python
from prefect import flow, Client
import requests
from requests.auth import HTTPBasicAuth
import os
# Assuming you have the following environment variables set
# PREFECT_API_URL: The URL to your Prefect Server
# PREFECT_API_KEY: The API Key for authentication
# (or PREFECT_USERNAME and PREFECT_PASSWORD for basic auth)
client = Client(api_server=os.getenv("PREFECT_API_URL"))
# Use token-based authentication as an example
api_key = os.getenv("PREFECT_API_KEY")
if api_key:
client._session.headers.update({"Authorization": f"Bearer {api_key}"})
else:
# Fall back to basic authentication
username = os.getenv("PREFECT_USERNAME")
password = os.getenv("PREFECT_PASSWORD")
if username and password:
auth = HTTPBasicAuth(username, password)
client._session.auth = auth
# Define your flow and deployment object just before serving them
if __name__ == "__main__":
repo_info_deploy = repo_info_to_deployment(
name="my-first-deployment",
cron="* * * * *",
tags=["testing", "tutorial"],
description="Given a GitHub repository, logs repository statistics for that repo.",
version="tutorial/deployments"
)
serve(
repo_info_deploy,
# You may need additional arguments based on your serve function's requirements
)
Keep in mind this code doesn't exactly show where the serve
function and the repo_info_to_deployment
function come from, which likely need to be imported or defined in your code.
Please replace repo_info_to_deployment
with your actual function or class responsible for creating a deployment object. The serve
function should also be the Prefect serving method or your equivalent to register the deployment with your Prefect installation.
Ensure that you never expose your API keys, passwords, or credentials in your source code. Always use a secure method to pass these values to your applications.
Again, this example doesn't link to a specific Prefect example as it's a more general Python pattern for handling Basic or Bearer token authentication. Adjust it as necessary to suit the details of how your reverse proxy handles authentication.Nate
02/24/2024, 6:06 PMPREFECT_API_URL
as an env var that correctly points to your server according to your networking
you dont often need to do this in codeNate
02/24/2024, 6:07 PMClient
stuff marvin is doing above is complete nonsense but this is right
# Assuming you have the following environment variables set
# PREFECT_API_URL: The URL to your Prefect Server
# PREFECT_API_KEY: The API Key for authentication
Emmanuel Rambeau
02/24/2024, 6:56 PMNate
02/24/2024, 6:57 PM