From dda78df9d2c8d45f1b0088aaab07bce27104394d Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 7 May 2015 06:39:34 +0200 Subject: [PATCH] Add new action queue, synchronized with main thread for prompt like actions (conf loading, exit, ...) --- nemubot/__main__.py | 3 +-- nemubot/bot.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/nemubot/__main__.py b/nemubot/__main__.py index 5d0ef6a..d1d8f93 100644 --- a/nemubot/__main__.py +++ b/nemubot/__main__.py @@ -104,8 +104,7 @@ def main(): # Load requested configuration files for path in args.files: if os.path.isfile(path): - from nemubot.tools.config import load_file - load_file(path, context) + context.sync_queue.put_nowait(["loadconf", path]) else: logger.error("%s is not a readable file", path) diff --git a/nemubot/bot.py b/nemubot/bot.py index 259d368..41f619b 100644 --- a/nemubot/bot.py +++ b/nemubot/bot.py @@ -125,11 +125,13 @@ class Bot(threading.Thread): return res self.hooks.add_hook(nemubot.hooks.Message(_help_msg, "help"), "in", "Command") - # Messages to be treated from queue import Queue + # Messages to be treated self.cnsr_queue = Queue() self.cnsr_thrd = list() self.cnsr_thrd_size = -1 + # Synchrone actions to be treated by main thread + self.sync_queue = Queue() def run(self): @@ -190,6 +192,16 @@ class Bot(threading.Thread): self.cnsr_thrd.append(c) c.start() + while self.sync_queue.qsize() > 0: + action = self.sync_queue.get_nowait() + if action[0] == "exit": + self.quit() + elif action[0] == "loadconf": + for path in action[1:]: + from nemubot.tools.config import load_file + load_file(path, self) + self.sync_queue.task_done() + # Events methods