From 6dfab37ef856fade2839dd0315db4f2d25284947 Mon Sep 17 00:00:00 2001 From: Arksine Date: Wed, 11 Nov 2020 04:39:53 -0500 Subject: [PATCH] moonraker: spawn remote methods on the event loop This allows regsitered methods to be coroutines. Execution is done on the event loop to prevent a coroutine from blocking the incoming command queue. Signed-off-by: Eric Callahan --- moonraker/moonraker.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index e438602..7e0a9ed 100644 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -183,10 +183,10 @@ class Server: method = cmd.get('method', None) if method is not None: # This is a remote method called from klippy - cb = self.remote_methods.get(method, None) - if cb is not None: + if method in self.remote_methods: params = cmd.get('params', {}) - cb(**params) + self.ioloop.spawn_callback( + self._execute_method, method, **params) else: logging.info(f"Unknown method received: {method}") return @@ -207,6 +207,14 @@ class Server: result = ServerError(err, 400) request.notify(result) + async def _execute_method(self, method_name, **kwargs): + try: + ret = self.remote_methods[method_name](**kwargs) + if asyncio.iscoroutine(ret): + await ret + except Exception: + logging.exception(f"Error running remote method: {method_name}") + def on_connection_closed(self): self.init_list = [] self.klippy_state = "disconnected"