moonraker: move loop creation to the eventloop module

Python 3.10 deprecates "get_event_loop()", so it is necessary to
create a new loop and set it as active at startup.  Previously
Moonraker only did this after a soft restart.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-07-16 19:41:09 -04:00
parent d37f91c9c8
commit 53a3c64186
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
2 changed files with 18 additions and 13 deletions

View File

@ -9,6 +9,8 @@ import asyncio
import inspect
import functools
import socket
import time
import logging
from typing import (
TYPE_CHECKING,
Awaitable,
@ -31,7 +33,7 @@ class EventLoop:
self.reset()
def reset(self) -> None:
self.aioloop = asyncio.get_event_loop()
self.aioloop = self._create_new_loop()
self.add_signal_handler = self.aioloop.add_signal_handler
self.remove_signal_handler = self.aioloop.remove_signal_handler
self.add_reader = self.aioloop.add_reader
@ -45,6 +47,21 @@ class EventLoop:
self.set_debug = self.aioloop.set_debug
self.is_running = self.aioloop.is_running
def _create_new_loop(self) -> asyncio.AbstractEventLoop:
for _ in range(5):
# Sometimes the new loop does not properly instantiate.
# Give 5 attempts before raising an exception
new_loop = asyncio.new_event_loop()
if not new_loop.is_closed():
break
logging.info("Failed to create open eventloop, "
"retyring in .5 seconds...")
time.sleep(.5)
else:
raise RuntimeError("Unable to create new open eventloop")
asyncio.set_event_loop(new_loop)
return new_loop
def register_callback(self,
callback: FlexCallback,
*args,

View File

@ -493,18 +493,6 @@ def main(cmd_line_args: argparse.Namespace) -> None:
# it is ok to use a blocking sleep here
time.sleep(.5)
logging.info("Attempting Server Restart...")
for _ in range(5):
# Sometimes the new loop does not properly instantiate.
# Give 5 attempts before raising an exception
new_loop = asyncio.new_event_loop()
if not new_loop.is_closed():
break
logging.info("Failed to create open eventloop, "
"retyring in .5 seconds...")
time.sleep(.5)
else:
raise RuntimeError("Unable to create new open eventloop")
asyncio.set_event_loop(new_loop)
event_loop.reset()
event_loop.close()
logging.info("Server Shutdown")