I want to run a starlette and django app in the same thread.
(Having them in same thread allows fast thread-local communication between them).
Considering that asgi apps are just coroutines, I thought this should be theoretically possible with asyncio.gather()
.
I came up with a little hack to make this work, but it has some limitations.
from uvicorn import Server, Config
configs = [Config(app1, uds='app1.sock'), Config(app2, uds='app2.sock')]
coros = [Server(c).serve() for c in configs]
await asyncio.gather(*coros)
- Doesn't support
reload
andworkers
options. - Ctrl+C only works for one app.
INFO: Started server process [86066]
INFO: Waiting for application startup.
INFO: Started server process [86066]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on unix socket app1.sock (Press CTRL+C to quit)
INFO: Application startup complete.
INFO: Uvicorn running on unix socket app2.sock (Press CTRL+C to quit)
^CINFO: Shutting down
INFO: Finished server process [86066]
^C^C^C^C^C
What's a better way to do this?