-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add fastapi dir and change dep to rocksdict * commit inital example * update example * use lifespan * reorganize to have an endpoint trigger a producer * reorganize timer to be above fast api decorator
- Loading branch information
Showing
2 changed files
with
73 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
django | ||
faust[rocksdb] | ||
faust[rocksdict] | ||
eventlet | ||
faust-aioeventlet |
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,72 @@ | ||
#!/usr/bin/env python | ||
import asyncio | ||
from contextlib import asynccontextmanager | ||
from typing import Union | ||
|
||
from fastapi import FastAPI | ||
|
||
import faust | ||
|
||
|
||
# This is just hello_world.py integrated with a FastAPI application | ||
|
||
|
||
def fake_answer_to_everything_ml_model(x: float): | ||
return x * 42 | ||
|
||
|
||
ml_models = {} | ||
|
||
|
||
# You MUST have "app" defined in order for Faust to discover the app | ||
# if you're using "faust" on CLI, but this doesn't work yet | ||
faust_app = faust.App( | ||
'hello-world-fastapi', | ||
broker='kafka://localhost:9092', | ||
web_enabled=False, | ||
) | ||
# app = faust_app | ||
|
||
greetings_topic = faust_app.topic('greetings', value_type=str) | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
# Load the ML model | ||
ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model | ||
await faust_app.start() | ||
yield | ||
# Clean up the ML models and release the resources | ||
ml_models.clear() | ||
await faust_app.stop() | ||
|
||
|
||
app = fastapi_app = FastAPI( | ||
lifespan=lifespan, | ||
) | ||
# For now, run via "uvicorn fastapi_example:app" | ||
# then visit http://127.0.0.1:8000/docs | ||
|
||
|
||
@fastapi_app.get("/") | ||
def read_root(): | ||
return {"Hello": "World"} | ||
|
||
|
||
@fastapi_app.get("/items/{item_id}") | ||
def read_item(item_id: int, q: Union[str, None] = None): | ||
return {"item_id": item_id, "q": q} | ||
|
||
|
||
@faust_app.agent(greetings_topic) | ||
async def print_greetings(greetings): | ||
async for greeting in greetings: | ||
print(greeting) | ||
|
||
|
||
@faust_app.timer(5) # make sure you *always* add the timer above if you're using one | ||
@fastapi_app.get("/produce") | ||
async def produce(): | ||
for i in range(100): | ||
await greetings_topic.send(value=f'hello {i}') | ||
return {"success": True} |