-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using trio-asyncio with FastAPI #84
Comments
Writing all the details gave me enough clarity to solve this myself, so I'm answering myself StackOverflow style. The trick is to run uvicorn yourself with the right incantations. import fastapi
import trio
import trio_asyncio
import uvicorn
app = fastapi.FastAPI()
@app.get("/sleep/{seconds}")
async def sleep(seconds: float):
await trio_asyncio.trio_as_aio(trio.sleep)(seconds)
if __name__ == "__main__":
config = uvicorn.Config(app=app)
server = uvicorn.Server(config=config)
trio_asyncio.run(trio_asyncio.aio_as_trio(server.serve)) The usual entrypoint is If you want to deploy that using gunicorn, you can probably subclass |
I tried running this example in windows but it gives following error:
|
Two users in chat want to use Trio in FastAPI. FastAPI is quite popular in the async Python world, so I guess they're not the only one.
FastAPI recommends uvicorn for deployment. Unfortunately, uvicorn handles the asyncio event loop itself. How can I use uvicorn, FastAPI and trio-asyncio?
The text was updated successfully, but these errors were encountered: