From 28cdf05a475521240431b7ee122631e5a2292e43 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 23 Sep 2019 10:24:03 +0100 Subject: [PATCH] push markov loading half on to a background thread that takes the main thread for database actions --- modules/markov.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/modules/markov.py b/modules/markov.py index c5f9ff55..9e6a8eb7 100644 --- a/modules/markov.py +++ b/modules/markov.py @@ -1,4 +1,4 @@ -import random +import random, threading from src import ModuleManager, utils NO_MARKOV = "Markov chains not enabled in this channel" @@ -26,14 +26,27 @@ class Module(ModuleManager.BaseModule): if not event["target"].get_setting("markov", False): raise utils.EventError(NO_MARKOV) + if not self._load_thread == None: + raise utils.EventError("Log loading already in progress") + page = utils.http.request(event["args_split"][0]) if page.code == 200: - for line in page.data.decode("utf8").split("\n"): - self._create(event["target"].id, line.strip("\r").split(" ")) - event["stdout"].write("Log imported") + event["stdout"].write("Importing...") + self._load_thread = threading.Thread(target=self._load_loop, + args=[event["target"].id, page.data]) + self._load_thread.daemon = True + self._load_thread.start() else: event["stderr"].write("Failed to load log (%d)" % page.code) + def _load_loop(self, channel_id, data): + for line in data.decode("utf8").split("\n"): + line = line.strip("\r").split(" ") + self.bot.trigger(self._create_factory(channel_id, line)) + self._load_thread = None + def _create_factory(self, channel_id, line): + return lambda: self._create(channel_id, line) + def _create(self, channel_id, words): words = list(filter(None, words)) words = [word.lower() for word in words]