Last active
February 24, 2024 01:18
-
-
Save tenuki/ff67f87cba5c4c04fd08d9c800437477 to your computer and use it in GitHub Desktop.
How to run multiple uvicorn server apps in the same process (thanks @a-d-j-i )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## How to run multiple uvicorn server apps in the same process | |
## | |
import asyncio | |
from uvicorn import Server, Config | |
class MyServer(Server): | |
async def run(self, sockets=None): | |
self.config.setup_event_loop() | |
return await self.serve(sockets=sockets) | |
async def run(): | |
apps = [] | |
for cfg in configList: | |
config = Config("srcs.myapp:app", host="0.0.0.0", | |
port=cfg["port"]) | |
server = MyServer(config=config) | |
apps.append(server.run()) | |
return await asyncio.gather(*apps) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This did it for me, and its interruptible: