Skip to content

Instantly share code, notes, and snippets.

@simon-mo
Created June 7, 2022 17:57
Show Gist options
  • Select an option

  • Save simon-mo/a4199f9a5d9cd01a384689960558579c to your computer and use it in GitHub Desktop.

Select an option

Save simon-mo/a4199f9a5d9cd01a384689960558579c to your computer and use it in GitHub Desktop.
import uvicorn
from fastapi import FastAPI
import asyncio
import ray
from ray import serve
@ray.remote
def my_task():
print("in a training job")
def create_app():
app = FastAPI()
@app.get("/")
async def func():
ray.put(1)
await my_task.remote()
return "done!"
return app
@serve.deployment(route_prefix=None)
class DirectIngressApp:
async def __init__(self, port:int):
self.app = create_app()
config = uvicorn.Config(
self.app,
port=port,
access_log=True,
)
server = uvicorn.Server(config=config)
self.serving_task = asyncio.get_event_loop().create_task(server.serve())
# That's it, don't implement call method and don't call it explicitly
serve.start()
DirectIngressApp.deploy(port=9999)
while True:
import time
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment