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 <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-11-11 04:39:53 -05:00
parent a0e23eb22a
commit 6dfab37ef8
1 changed files with 11 additions and 3 deletions

View File

@ -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"