From 51f4d4b8885096f7e9eb69b9d902ba3db4128e11 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Tue, 27 Jun 2023 08:00:13 -0400 Subject: [PATCH] eventloop: add support for uvloop Signed-off-by: Eric Callahan --- moonraker/components/shell_command.py | 3 ++- moonraker/eventloop.py | 11 +++++++++++ moonraker/server.py | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/moonraker/components/shell_command.py b/moonraker/components/shell_command.py index abeaf6a..086df79 100644 --- a/moonraker/components/shell_command.py +++ b/moonraker/components/shell_command.py @@ -22,6 +22,7 @@ from typing import ( Coroutine, Dict, Set, + cast ) if TYPE_CHECKING: from ..confighelper import ConfigHelper @@ -61,8 +62,8 @@ class ShellCommandProtocol(asyncio.subprocess.SubprocessStreamProtocol): def connection_made( self, transport: asyncio.transports.BaseTransport ) -> None: + transport = cast(asyncio.SubprocessTransport, transport) self._transport = transport - assert isinstance(transport, asyncio.SubprocessTransport) stdout_transport = transport.get_pipe_transport(1) if stdout_transport is not None: self._pipe_fds.append(1) diff --git a/moonraker/eventloop.py b/moonraker/eventloop.py index 695bc72..ce12d94 100644 --- a/moonraker/eventloop.py +++ b/moonraker/eventloop.py @@ -5,6 +5,8 @@ # This file may be distributed under the terms of the GNU GPLv3 license from __future__ import annotations +import os +import contextlib import asyncio import inspect import functools @@ -21,12 +23,21 @@ from typing import ( Union ) +_uvl_var = os.getenv("MOONRAKER_ENABLE_UVLOOP", "y").lower() +_uvl_enabled = False +if _uvl_var in ["y", "yes", "true"]: + with contextlib.suppress(ImportError): + import uvloop + uvloop.install() + _uvl_enabled = True + if TYPE_CHECKING: _T = TypeVar("_T") FlexCallback = Callable[..., Optional[Awaitable]] TimerCallback = Callable[[float], Union[float, Awaitable[float]]] class EventLoop: + UVLOOP_ENABLED = _uvl_enabled TimeoutError = asyncio.TimeoutError def __init__(self) -> None: self.reset() diff --git a/moonraker/server.py b/moonraker/server.py index 9281b12..986bae1 100755 --- a/moonraker/server.py +++ b/moonraker/server.py @@ -586,6 +586,7 @@ def main(from_package: bool = True) -> None: app_args["log_file"] = str(data_path.joinpath("logs/moonraker.log")) app_args["python_version"] = sys.version.replace("\n", " ") app_args["msgspec_enabled"] = json_wrapper.MSGSPEC_ENABLED + app_args["uvloop_enabled"] = EventLoop.UVLOOP_ENABLED log_manager = LogManager(app_args, startup_warnings) # Start asyncio event loop and server