From a0458f7f831816409278d7eb85ac68be624a9204 Mon Sep 17 00:00:00 2001 From: Charles Mita Date: Wed, 22 Jun 2016 16:40:17 +0100 Subject: [PATCH] Add async option to add_spawn_function Some things may need to be started synchronously (if they're functions that themselves start an asynchronous loop for instance). --- malcolm/core/servercomms.py | 3 ++- malcolm/core/spawnable.py | 13 ++++++++----- tests/test_core/test_servercomms.py | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/malcolm/core/servercomms.py b/malcolm/core/servercomms.py index 0416db486..5e2f75835 100644 --- a/malcolm/core/servercomms.py +++ b/malcolm/core/servercomms.py @@ -12,7 +12,8 @@ def __init__(self, name, process): self.q = self.process.create_queue() self.add_spawn_function(self.send_loop, self.make_default_stop_func(self.q)) - self.add_spawn_function(self.start_recv_loop, self.stop_recv_loop) + self.add_spawn_function(self.start_recv_loop, self.stop_recv_loop, + async=False) def send_loop(self): """Service self.q, sending responses to client""" diff --git a/malcolm/core/spawnable.py b/malcolm/core/spawnable.py index 6f7ad5ecc..e7729d1ec 100644 --- a/malcolm/core/spawnable.py +++ b/malcolm/core/spawnable.py @@ -22,14 +22,17 @@ def start(self, process=None): if process is None: process = self.process self._initialize() - for (func, stop_func) in self._spawn_functions: - self._spawned.append(process.spawn(func)) + for (func, stop_func, async) in self._spawn_functions: + spawned = process.spawn(func) + self._spawned.append(spawned) + if not async: + spawned.wait() def stop(self): """Call registered stop functions""" self._initialize() - for (func, stop_func) in reversed(self._spawn_functions): + for (func, stop_func, async) in reversed(self._spawn_functions): if stop_func is not None: stop_func() @@ -39,7 +42,7 @@ def wait(self, timeout=None): spawned.wait(timeout=timeout) self._spawned = [] - def add_spawn_function(self, func, stop_func=None): + def add_spawn_function(self, func, stop_func=None, async=True): """Register functions to be triggered by self.start and self.stop Args: @@ -47,7 +50,7 @@ def add_spawn_function(self, func, stop_func=None): stop_func: function to halt the spawned function (default None) """ self._initialize() - self._spawn_functions.append((func, stop_func)) + self._spawn_functions.append((func, stop_func, async)) def make_default_stop_func(self, q): """Convenience function for creating a default stop function that puts diff --git a/tests/test_core/test_servercomms.py b/tests/test_core/test_servercomms.py index 902c84b3a..cae4ebee7 100644 --- a/tests/test_core/test_servercomms.py +++ b/tests/test_core/test_servercomms.py @@ -21,7 +21,7 @@ def test_init(self, def_stop, add_func): server.q, self.process.create_queue.return_value) self.assertEquals( [call(server.send_loop, def_stop.return_value), - call(server.start_recv_loop, server.stop_recv_loop)], + call(server.start_recv_loop, server.stop_recv_loop, async=False)], server.add_spawn_function.call_args_list) def test_not_implemented_error(self):