2020-03-26 04:42:46 +00:00
|
|
|
import asyncio
|
|
|
|
import signal
|
|
|
|
|
2020-01-20 16:29:06 +00:00
|
|
|
from sys import argv
|
|
|
|
|
2019-09-22 21:55:36 +01:00
|
|
|
from multidict import CIMultiDict # type: ignore
|
2019-07-19 03:57:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Header(CIMultiDict):
|
2019-07-19 04:11:25 +01:00
|
|
|
def get_all(self, key):
|
|
|
|
return self.getall(key, default=[])
|
2020-01-20 16:29:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
use_trio = argv[0].endswith("hypercorn") and "trio" in argv
|
|
|
|
|
|
|
|
if use_trio:
|
2020-07-07 14:13:03 +01:00
|
|
|
from trio import Path # type: ignore
|
|
|
|
from trio import open_file as open_async # type: ignore
|
2020-01-20 16:29:06 +00:00
|
|
|
|
|
|
|
def stat_async(path):
|
|
|
|
return Path(path).stat()
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
from aiofiles import open as aio_open # type: ignore
|
|
|
|
from aiofiles.os import stat as stat_async # type: ignore # noqa: F401
|
|
|
|
|
|
|
|
async def open_async(file, mode="r", **kwargs):
|
|
|
|
return aio_open(file, mode, **kwargs)
|
2020-03-26 04:42:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ctrlc_workaround_for_windows(app):
|
|
|
|
async def stay_active(app):
|
|
|
|
"""Asyncio wakeups to allow receiving SIGINT in Python"""
|
|
|
|
while not die:
|
|
|
|
# If someone else stopped the app, just exit
|
|
|
|
if app.is_stopping:
|
|
|
|
return
|
|
|
|
# Windows Python blocks signal handlers while the event loop is
|
|
|
|
# waiting for I/O. Frequent wakeups keep interrupts flowing.
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
# Can't be called from signal handler, so call it from here
|
|
|
|
app.stop()
|
|
|
|
|
|
|
|
def ctrlc_handler(sig, frame):
|
|
|
|
nonlocal die
|
|
|
|
if die:
|
|
|
|
raise KeyboardInterrupt("Non-graceful Ctrl+C")
|
|
|
|
die = True
|
|
|
|
|
|
|
|
die = False
|
|
|
|
signal.signal(signal.SIGINT, ctrlc_handler)
|
|
|
|
app.add_task(stay_active)
|