From c0afd99e4651d5f460fb2c691a93783b26a7488a Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 11 Jan 2022 20:53:59 +0100 Subject: [PATCH] First stab at tackling issues involved in running Syndicate in a multi-threaded context --- syndicate/__init__.py | 11 +++++++---- syndicate/actor.py | 15 +++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/syndicate/__init__.py b/syndicate/__init__.py index 6ca6ac2..24156da 100644 --- a/syndicate/__init__.py +++ b/syndicate/__init__.py @@ -12,11 +12,14 @@ def __setup(): class turn: @staticproperty def active(): - return _active.turn + t = getattr(_active, 'turn', False) + if t is False: + t = _active.turn = None + return t @staticproperty def log(): - return _active.turn.log + return turn.active.log def run(facet, action): Turn.run(facet, action) @@ -25,11 +28,11 @@ def __setup(): Turn.external(facet, action, loop=loop) def active_facet(): - return _active.turn._facet + return turn.active._facet def install_definition(name, definition): def handler(*args, **kwargs): - return definition(_active.turn, *args, **kwargs) + return definition(turn.active, *args, **kwargs) setattr(turn, name, handler) for (name, definition) in Turn.__dict__.items(): diff --git a/syndicate/actor.py b/syndicate/actor.py index 4242c88..290be59 100644 --- a/syndicate/actor.py +++ b/syndicate/actor.py @@ -284,7 +284,10 @@ def queue_task_threadsafe(thunk, loop = None): class Turn: @staticproperty def active(): - return _active.turn + t = getattr(_active, 'turn', False) + if t is False: + t = _active.turn = None + return t @classmethod def run(cls, facet, action, zombie_turn = False): @@ -551,7 +554,11 @@ def __boot_inert(): async def __run_inert(): Actor(__boot_inert, name = '_inert_actor') def __setup_inert(): - loop = asyncio.new_event_loop() - loop.run_until_complete(__run_inert()) - loop.close() + def setup_main(): + loop = asyncio.new_event_loop() + loop.run_until_complete(__run_inert()) + loop.close() + t = threading.Thread(target=setup_main) + t.start() + t.join() __setup_inert()