Why does the asyncio’s event loop suppress the KeyboardInterrupt on Windows?

There is workaround for Windows. Run another corouting which wake up loop every second and allow loop to react on keyboard interrupt

Example with Echo server from asyncio doc

async def wakeup():
    while True:
        await asyncio.sleep(1)

loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)

# add wakeup HACK
loop.create_task(wakeup())

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

Leave a Comment