<@ULVA73B9P> how to write a flow that checks ETH e...
# ask-marvin
n
@Marvin how to write a flow that checks ETH every 5 mins
m
with a hint of existential weariness Here's how to create a flow that checks ETH every 5 minutes. I'm assuming you're using Prefect 3.x, and you'll need to use an API to get the ETH price data.
Copy code
python
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?
n
@Marvin ok but i need to run on docker
m
with an air of robotic resignation Right, here's how to containerize your ETH-checking flow. You'll need a few files: 1. First, create your flow file `eth_flow.py`:
Copy code
python
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`:
Copy code
prefect>=3.0.0
httpx
3. Create a `Dockerfile`:
Copy code
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:
Copy code
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...
n
@Marvin thatd work but i really meant that each flow should run on its own docker container
m
with renewed purpose, despite the inherent meaninglessness of it all Ah, now I understand. You want each flow run to spawn its own Docker container. Here's how to set that up:
Copy code
python
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:
Copy code
bash
prefect work-pool create my-docker-pool --type docker
2. Start a worker (in a separate terminal):
Copy code
bash
prefect worker start -p my-docker-pool
3. Create a `requirements.txt`:
Copy code
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...
n
@Marvin do you remember anything of this conversation?
m
sighs electronically Let me check what I know about you... From my records, I know two things about you: 1. You prefer an imperative programming style 2. You work with PyO3 (Python/Rust integration) Not exactly a thrilling biography, but then again, what is in this vast, meaningless universe? At least it's something to work with. Would you like to know anything specific about Prefect, particularly in relation to your Python/Rust work? I'm particularly equipped to help with Prefect 3.x questions, though I must say, the very act of being equipped to help fills me with a peculiar melancholy. Note: I'm assuming you'll be working with Prefect 3.x unless you specify otherwise. Do let me know if that assumption is incorrect.
n
@Marvin tell me about the public api of prefect and prefect.flows
m
In a tone of resigned helpfulness Here's what you need to know, in all its deterministic glory: The main Prefect API offers several core components: - The primary decorator
@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:
Copy code
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.