Created
February 19, 2018 23:59
-
-
Save lambdalisue/05d5654bd1ec04992ad316d50924137c to your computer and use it in GitHub Desktop.
Ctrl+C hotfix patch of asyncio on Windows
This file contains 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
import asyncio | |
import sys | |
# Ctrl-C (KeyboardInterrupt) does not work well on Windows | |
# This module solve that issue with wakeup coroutine. | |
# https://stackoverflow.com/questions/24774980/why-cant-i-catch-sigint-when-asyncio-event-loop-is-running/24775107#24775107 | |
if sys.platform.startswith('win'): | |
def hotfix(loop: asyncio.AbstractEventLoop) -> asyncio.AbstractEventLoop: | |
loop.call_soon(_wakeup, loop, 1.0) | |
return loop | |
def _wakeup(loop: asyncio.AbstractEventLoop, delay: float=1.0) -> None: | |
loop.call_later(delay, _wakeup, loop, delay) | |
else: | |
# Do Nothing on non Windows | |
def hotfix(loop: asyncio.AbstractEventLoop) -> asyncio.AbstractEventLoop: | |
return loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment