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:
parent
d37f91c9c8
commit
53a3c64186
|
@ -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,
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue