-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# /// script | ||
# dependencies ["prefect"] | ||
# /// | ||
|
||
""" | ||
This example shows how to bind specific parameters to a schedule. | ||
We check the price of BTC and ETH at an interval of 10 seconds, and the price of SOL and | ||
DOGE according to a cron schedule. | ||
""" | ||
|
||
from datetime import timedelta | ||
from typing import Any | ||
|
||
import httpx | ||
from prefect import flow, task | ||
from prefect.schedules import Cron, Interval | ||
|
||
|
||
@task | ||
def fetch_crypto_price(crypto_id: str) -> dict[str, Any]: | ||
"""Fetch current price for a cryptocurrency.""" | ||
with httpx.Client() as client: | ||
response = client.get( | ||
"https://api.coingecko.com/api/v3/simple/price", | ||
params={"ids": crypto_id, "vs_currencies": "usd"}, | ||
) | ||
return response.json() | ||
|
||
|
||
@flow(log_prints=True, flow_run_name="check prices: {currencies_to_check}") | ||
def get_crypto_prices(currencies_to_check: list[str]): | ||
"""Display current prices for provided cryptocurrencies.""" | ||
|
||
for price in fetch_crypto_price.map(currencies_to_check).result(): | ||
_id = list(price.keys())[0] | ||
print(f"{_id}: ${price[_id]['usd']:,.2f}") | ||
|
||
|
||
if __name__ == "__main__": | ||
get_crypto_prices.serve( | ||
schedules=[ | ||
Interval( | ||
timedelta(seconds=10), | ||
timezone="America/New_York", | ||
parameters={"currencies_to_check": ["bitcoin", "ethereum"]}, | ||
), | ||
Cron( | ||
"42 0 6 9 *", | ||
timezone="America/New_York", | ||
parameters={"currencies_to_check": ["solana", "dogecoin"]}, | ||
), | ||
] | ||
) |