Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Created February 19, 2018 23:59
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lambdalisue/05d5654bd1ec04992ad316d50924137c to your computer and use it in GitHub Desktop.
Ctrl+C hotfix patch of asyncio on Windows
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