I'm working on a Python app for a university project, that uses Kivy as the UI framework. We're using the async event-loop like this:
import asyncio
from ui.app import SuperhirnApp
app = SuperhirnApp()
loop = asyncio.get_event_loop()
loop.run_until_complete(app.async_run(async_lib="asyncio"))
loop.close()
This works perfectly fine. I'm now trying to add FastAPI to the application to send and receive network requests, to make the game multiplayer-capable.
I've installed FastAPI
and uvicorn
to run a webserver, but I'm struggling to make Kivy and uvicorn
run at the same time. Here's what I tried:
import asyncio
from fastapi import FastAPI
from ui.app import SuperhirnApp
import uvicorn
api = FastAPI()
app = SuperhirnApp(api=api)
uvicorn.run("main:api", host="0.0.0.0", port=8000)
loop = asyncio.get_event_loop()
loop.run_until_complete(app.async_run(async_lib="asyncio"))
loop.close()
When running this with python src/main.py
, it crashes right away with the following error:
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'Server.serve' was never awaited
How can I make asyncio
run Kivy and FastAPI/uvicorn run in parallel?