From c48af9a891e1344f72912d4abb7e246be6d0a696 Mon Sep 17 00:00:00 2001 From: Eriks Karls Date: Wed, 30 Oct 2019 18:16:18 +0200 Subject: [PATCH] Thread stopping --- erepublik/citizen.py | 2 +- erepublik/classes.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/erepublik/citizen.py b/erepublik/citizen.py index 2e67846..b0dccee 100644 --- a/erepublik/citizen.py +++ b/erepublik/citizen.py @@ -63,8 +63,8 @@ class Citizen(classes.CitizenAPI): self.my_companies = classes.MyCompanies() self.set_debug(True) self.reporter = classes.Reporter() - self.telegram = classes.TelegramBot() self.stop_threads = threading.Event() + self.telegram = classes.TelegramBot(stop_event=self.stop_threads) if auto_login: self.login() diff --git a/erepublik/classes.py b/erepublik/classes.py index 601eada..c6f122a 100644 --- a/erepublik/classes.py +++ b/erepublik/classes.py @@ -1128,11 +1128,15 @@ class TelegramBot: chat_id = 0 api_url = "" player_name = "" + __thread_stopper: threading.Event = None _last_time: datetime.datetime = None _last_full_energy_report: datetime.datetime = None _next_time: datetime.datetime = None _threads: List[threading.Thread] = [] + def __init__(self, stop_event: threading.Event = None): + self.__thread_stopper = threading.Event() if stop_event is None else stop_event + def __dict__(self): return dict(chat_id=self.chat_id, api_url=self.api_url, player=self.player_name, last_time=self._last_time, next_time=self._next_time, queue=self.__queue, initialized=self.__initialized, @@ -1189,7 +1193,9 @@ class TelegramBot: def __send_messages(self): while self._next_time > utils.now(): - utils.silent_sleep(utils.get_sleep_seconds(self._next_time)) + if self.__thread_stopper.is_set(): + break + self.__thread_stopper.wait(utils.get_sleep_seconds(self._next_time)) message = "\n\n––––––––––––––––––––––\n\n".join(self.__queue) if self.player_name: