aiohttp Server Stops Immediately

Viewed 94

I am trying to run a simple aiohttp server that can be stopped by the user hitting CTRL+C.

However, right now it appears to stop immediately with no error messages after starting. What might be the problem?

import asyncio
from aiohttp import web

class Foo:
    def __init__(self):
        self.app = web.Application()
        self.app.add_routes([])
        self.runner = web.AppRunner(self.app)

    async def start(self):
        await self.runner.setup()
        site = web.TCPSite(self.runner, "127.0.0.1", 1234)
        await site.start()

    async def stop(self):
        await self.runner.cleanup()

async def main():
    await foo.start()

if __name__ == '__main__':
    foo = Foo()
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        asyncio.run(foo.stop())
0 Answers
Related