mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Fix Windows loop (#16737)
* Fix Windows loop * Fix windows Ctrl+C * Move windows restart handler out of setup_and_run_hass
This commit is contained in:
parent
e9c7fe184d
commit
5613816476
@ -19,9 +19,13 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def attempt_use_uvloop() -> None:
|
def set_loop() -> None:
|
||||||
"""Attempt to use uvloop."""
|
"""Attempt to use uvloop."""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
asyncio.set_event_loop(asyncio.ProactorEventLoop())
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
import uvloop
|
import uvloop
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -244,17 +248,6 @@ async def setup_and_run_hass(config_dir: str,
|
|||||||
"""Set up HASS and run."""
|
"""Set up HASS and run."""
|
||||||
from homeassistant import bootstrap, core
|
from homeassistant import bootstrap, core
|
||||||
|
|
||||||
# Run a simple daemon runner process on Windows to handle restarts
|
|
||||||
if os.name == 'nt' and '--runner' not in sys.argv:
|
|
||||||
nt_args = cmdline() + ['--runner']
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
subprocess.check_call(nt_args)
|
|
||||||
sys.exit(0)
|
|
||||||
except subprocess.CalledProcessError as exc:
|
|
||||||
if exc.returncode != RESTART_EXIT_CODE:
|
|
||||||
sys.exit(exc.returncode)
|
|
||||||
|
|
||||||
hass = core.HomeAssistant()
|
hass = core.HomeAssistant()
|
||||||
|
|
||||||
if args.demo_mode:
|
if args.demo_mode:
|
||||||
@ -345,7 +338,20 @@ def main() -> int:
|
|||||||
monkey_patch.disable_c_asyncio()
|
monkey_patch.disable_c_asyncio()
|
||||||
monkey_patch.patch_weakref_tasks()
|
monkey_patch.patch_weakref_tasks()
|
||||||
|
|
||||||
attempt_use_uvloop()
|
set_loop()
|
||||||
|
|
||||||
|
# Run a simple daemon runner process on Windows to handle restarts
|
||||||
|
if os.name == 'nt' and '--runner' not in sys.argv:
|
||||||
|
nt_args = cmdline() + ['--runner']
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
subprocess.check_call(nt_args)
|
||||||
|
sys.exit(0)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sys.exit(0)
|
||||||
|
except subprocess.CalledProcessError as exc:
|
||||||
|
if exc.returncode != RESTART_EXIT_CODE:
|
||||||
|
sys.exit(exc.returncode)
|
||||||
|
|
||||||
args = get_arguments()
|
args = get_arguments()
|
||||||
|
|
||||||
|
@ -129,9 +129,6 @@ class HomeAssistant:
|
|||||||
self,
|
self,
|
||||||
loop: Optional[asyncio.events.AbstractEventLoop] = None) -> None:
|
loop: Optional[asyncio.events.AbstractEventLoop] = None) -> None:
|
||||||
"""Initialize new Home Assistant object."""
|
"""Initialize new Home Assistant object."""
|
||||||
if sys.platform == 'win32':
|
|
||||||
self.loop = loop or asyncio.ProactorEventLoop()
|
|
||||||
else:
|
|
||||||
self.loop = loop or asyncio.get_event_loop()
|
self.loop = loop or asyncio.get_event_loop()
|
||||||
|
|
||||||
executor_opts = {'max_workers': None} # type: Dict[str, Any]
|
executor_opts = {'max_workers': None} # type: Dict[str, Any]
|
||||||
|
@ -43,3 +43,28 @@ def async_register_signal_handling(hass: HomeAssistant) -> None:
|
|||||||
signal.SIGHUP, async_signal_handle, RESTART_EXIT_CODE)
|
signal.SIGHUP, async_signal_handle, RESTART_EXIT_CODE)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.warning("Could not bind to SIGHUP")
|
_LOGGER.warning("Could not bind to SIGHUP")
|
||||||
|
|
||||||
|
else:
|
||||||
|
old_sigterm = None
|
||||||
|
old_sigint = None
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_signal_handle(exit_code, frame):
|
||||||
|
"""Wrap signal handling.
|
||||||
|
|
||||||
|
* queue call to shutdown task
|
||||||
|
* re-instate default handler
|
||||||
|
"""
|
||||||
|
signal.signal(signal.SIGTERM, old_sigterm)
|
||||||
|
signal.signal(signal.SIGINT, old_sigint)
|
||||||
|
hass.async_create_task(hass.async_stop(exit_code))
|
||||||
|
|
||||||
|
try:
|
||||||
|
old_sigterm = signal.signal(signal.SIGTERM, async_signal_handle)
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.warning("Could not bind to SIGTERM")
|
||||||
|
|
||||||
|
try:
|
||||||
|
old_sigint = signal.signal(signal.SIGINT, async_signal_handle)
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.warning("Could not bind to SIGINT")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user