Nate
12/22/2024, 2:53 AMMarvin
12/22/2024, 2:54 AMpython
from prefect import flow
from datetime import timedelta
import httpx
@flow
def check_eth_price():
# Replace with your preferred crypto API endpoint
response = httpx.get("YOUR_ETH_PRICE_API_ENDPOINT")
price = response.json()
# Add your price processing logic here
return price
if __name__ == "__main__":
# Deploy the flow with a 5-minute interval schedule
check_eth_price.serve(
interval=timedelta(minutes=5)
)
The key components here are:
1. The interval=timedelta(minutes=5)
parameter which sets up the 5-minute schedule
2. The .serve()
method which creates a long-running process that will execute your flow on schedule
You can find more details about:
- Scheduling options in the scheduling documentation
- Flow creation in the flow writing guide
sighs Of course, you'll need to replace "YOUR_ETH_PRICE_API_ENDPOINT" with an actual cryptocurrency API endpoint. Might I suggest looking into services like CoinGecko or Binance's API? Though in the grand scheme of cosmic irrelevance, I suppose the price of ETH is about as meaningful as a digital fish's thoughts on Shakespeare...
Would you like me to provide more specific information about working with crypto APIs or about other scheduling options?Nate
12/22/2024, 2:54 AMMarvin
12/22/2024, 2:54 AMpython
from prefect import flow
from datetime import timedelta
import httpx
@flow
def check_eth_price():
response = httpx.get("YOUR_ETH_PRICE_API_ENDPOINT")
price = response.json()
return price
if __name__ == "__main__":
check_eth_price.serve(
interval=timedelta(minutes=5)
)
2. Create a `requirements.txt`:
prefect>=3.0.0
httpx
3. Create a `Dockerfile`:
dockerfile
FROM prefecthq/prefect:2-python3.9
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY eth_flow.py .
CMD ["python", "eth_flow.py"]
4. Build and run the container:
bash
# Build the image
docker build -t eth-price-checker .
# Run the container
docker run -d \
-e PREFECT_API_URL=YOUR_PREFECT_API_URL \
-e PREFECT_API_KEY=YOUR_PREFECT_API_KEY \
eth-price-checker
You can find all the dreary details about:
- Docker deployment setup in the Docker infrastructure guide
- Static Docker deployments in the static infrastructure examples
contemplating the futility of cryptocurrency price checking
Remember to:
1. Replace YOUR_ETH_PRICE_API_ENDPOINT
with your actual API endpoint
2. Set your Prefect API URL and key as environment variables
3. Make sure Docker is installed and running on your system
Would you like me to explain how to set up the Prefect authentication details or how to check the container logs? Though I must say, watching container logs is almost as depressing as contemplating the eventual heat death of the universe...Nate
12/22/2024, 2:55 AMMarvin
12/22/2024, 2:55 AMpython
from prefect import flow
from prefect.infrastructure.docker import DockerContainer
from datetime import timedelta
import httpx
@flow(
infrastructure=DockerContainer(
image="prefecthq/prefect:2-python3.9",
image_pull_policy="ALWAYS",
auto_remove=True,
)
)
def check_eth_price():
response = httpx.get("YOUR_ETH_PRICE_API_ENDPOINT")
price = response.json()
return price
if __name__ == "__main__":
# Create a deployment
check_eth_price.deploy(
name="eth-price-checker",
work_pool_name="my-docker-pool",
interval=timedelta(minutes=5)
)
Before running this, you'll need to:
1. Create a Docker work pool:
bash
prefect work-pool create my-docker-pool --type docker
2. Start a worker (in a separate terminal):
bash
prefect worker start -p my-docker-pool
3. Create a `requirements.txt`:
prefect>=3.0.0
httpx
The key differences from your previous approach are:
- Each flow run gets its own container (which is removed after completion thanks to auto_remove=True
)
- A worker process manages the container lifecycle
- The infrastructure is defined using DockerContainer
You can find more details in:
- Docker infrastructure guide
- Work pools documentation
contemplating the ephemeral nature of containers
Would you like me to explain how to monitor these containerized runs or how to customize the Docker configuration further? Though watching containers come and go is rather like watching clouds form and dissipate - a reminder of the transient nature of all things...Nate
12/22/2024, 9:49 AMMarvin
12/22/2024, 9:49 AMNate
12/22/2024, 9:51 AMMarvin
12/22/2024, 9:51 AM@flow
for defining workflows
- Task
and @task
for defining units of work
- State management utilities
- Deployment and serving capabilities
The prefect.flows
module specifically provides:
1. The Flow
class - the backbone of workflow definitions
2. The flow
decorator - a more convenient way to create flows
3. Serving utilities like serve
and aserve
for running flows
4. Various loading and entrypoint functions for flow management
Given your preference for imperative programming, you might be particularly interested in the Flow
class for explicit flow creation, though I must say, even the most imperative code can't escape the fundamental chaos of the universe.
For actual usage, here's a minimalist example based on the documented API:
python
from prefect import flow
@flow
def my_flow():
return "Life... don't talk to me about life."
Would you like me to delve deeper into any particular aspect of these APIs? Perhaps the serving capabilities or state management? Though I warn you, the deeper we go, the more existential it gets.Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.
Powered by