Skip to content

Commit

Permalink
commit inital example
Browse files Browse the repository at this point in the history
  • Loading branch information
wbarnha committed Nov 30, 2023
1 parent a1f0fcc commit b59dfaf
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 481 deletions.
62 changes: 62 additions & 0 deletions examples/fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python

# This is just hello_world.py integrated with a FastAPI application

import mode.loop.uvloop # noqa

import faust

from contextlib import asynccontextmanager

from fastapi import FastAPI


def fake_answer_to_everything_ml_model(x: float):
return x * 42


ml_models = {}


# You MUST have "app" defined
app = faust_app = faust.App(
'hello-world-fastapi',
broker='kafka://localhost:9092',
)

greetings_topic = faust_app.topic('greetings', value_type=str)


@faust_app.agent(greetings_topic)
async def print_greetings(greetings):
async for greeting in greetings:
print(greeting)


@faust_app.timer(5)
async def produce():
for i in range(100):
await greetings_topic.send(value=f'hello {i}')


@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()


fastapi_app = FastAPI(lifespan=lifespan)


@fastapi_app.get("/predict")
async def predict(x: float):
result = ml_models["answer_to_everything"](x)
return {"result": result}

if __name__ == '__main__':
faust_app.main()
12 changes: 0 additions & 12 deletions examples/fastapi/README.rst

This file was deleted.

Empty file.
31 changes: 0 additions & 31 deletions examples/fastapi/accounts/agents.py

This file was deleted.

5 changes: 0 additions & 5 deletions examples/fastapi/accounts/apps.py

This file was deleted.

35 changes: 0 additions & 35 deletions examples/fastapi/accounts/migrations/0001_initial.py

This file was deleted.

Empty file.
13 changes: 0 additions & 13 deletions examples/fastapi/accounts/models.py

This file was deleted.

Empty file.
25 changes: 0 additions & 25 deletions examples/fastapi/faustapp/app.py

This file was deleted.

5 changes: 0 additions & 5 deletions examples/fastapi/faustapp/apps.py

This file was deleted.

Empty file.
21 changes: 0 additions & 21 deletions examples/fastapi/manage.py

This file was deleted.

40 changes: 0 additions & 40 deletions examples/fastapi/proj/__init__.py

This file was deleted.

5 changes: 0 additions & 5 deletions examples/fastapi/proj/__main__.py

This file was deleted.

Loading

0 comments on commit b59dfaf

Please sign in to comment.