Thread stopping

This commit is contained in:
Eriks Karls
2019-10-30 18:16:18 +02:00
parent 1abfdb71ac
commit c48af9a891
2 changed files with 8 additions and 2 deletions

View File

@ -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()

View File

@ -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: