From 2a7502e8e856f6d0f3a0e02e7e23e8960be3f9c7 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Fri, 1 Sep 2017 20:43:40 +0200 Subject: [PATCH] WIP Try to fix asyncio events add during asyncio event execution --- nemubot/bot.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/nemubot/bot.py b/nemubot/bot.py index 0638e95..e400e34 100644 --- a/nemubot/bot.py +++ b/nemubot/bot.py @@ -291,12 +291,7 @@ class Bot(threading.Thread): sync_act("launch_consumer") evt.start(self.loop) - - @asyncio.coroutine - def _add_event(): - return self.loop.call_at(evt._next, _end_event_timer, evt) - future = asyncio.run_coroutine_threadsafe(_add_event(), loop=self.loop) - evt.handle = future.result() + evt.handle = call_at(evt._next, _end_event_timer, evt) logger.debug("New event registered in %ss", evt._next - self.loop.time()) @@ -344,6 +339,17 @@ class Bot(threading.Thread): def add_module(self, module): """Add a module to the context, if already exists, unload the old one before""" + + import nemubot.hooks + + self.loop.call_soon_threadsafe(self._add_module, + module, + nemubot.hooks.hook.last_registered) + + nemubot.hooks.hook.last_registered = [] + + + def _add_module(self, module, registered_functions): module_name = module.__spec__.name if hasattr(module, "__spec__") else module.__name__ if hasattr(self, "stop") and self.stop: @@ -375,10 +381,8 @@ class Bot(threading.Thread): module.__dict__[attr] = module.__nemubot_context__ # Register decorated functions - import nemubot.hooks - for s, h in nemubot.hooks.hook.last_registered: + for s, h in registered_functions: module.__nemubot_context__.add_hook(h, *s if isinstance(s, list) else s) - nemubot.hooks.hook.last_registered = [] # Launch the module if hasattr(module, "load"):