make Communication thread safe

master
BodgeMaster 2022-03-20 10:34:56 +01:00
parent bcd9d00a47
commit a9e31d01f9
1 changed files with 15 additions and 1 deletions

16
util.py
View File

@ -1,4 +1,4 @@
import sys, traceback import sys, traceback, threading
EXIT_SUCCESS=0 EXIT_SUCCESS=0
EXIT_ERROR=1 EXIT_ERROR=1
@ -30,10 +30,12 @@ def error(message, is_exception=True, handle_gracefully=True):
#TODO: make thread safe #TODO: make thread safe
class Communication: class Communication:
def __init__(self): def __init__(self):
self.lock = threading.Lock()
self.__messages = {} self.__messages = {}
# send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default, a function to run as an additional action can be provided # send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default, a function to run as an additional action can be provided
def send(self, name, content, reverse_order=True, additional_action=None): def send(self, name, content, reverse_order=True, additional_action=None):
self.lock.acquire()
if name in self.__messages: if name in self.__messages:
if reverse_order: if reverse_order:
self.__messages[name] = [content] + self.__messages[name] self.__messages[name] = [content] + self.__messages[name]
@ -45,32 +47,44 @@ class Communication:
pass pass
else: else:
additional_action() additional_action()
self.lock.release()
# get the content of the first message tagged with name, removes the returned message by default # get the content of the first message tagged with name, removes the returned message by default
def get(self, name, remove=True): def get(self, name, remove=True):
self.lock.acquire()
if name in self.__messages and len(self.__messages[name])>0: if name in self.__messages and len(self.__messages[name])>0:
if remove: if remove:
content = self.__messages[name].pop(0) content = self.__messages[name].pop(0)
if len(self.__messages[name])==0: if len(self.__messages[name])==0:
del self.__messages[name] del self.__messages[name]
self.lock.release()
return content return content
else: else:
self.lock.release()
return self.__messages[name][0] return self.__messages[name][0]
else: else:
self.lock.release()
return None return None
# get the contents for all messages tagged with name # get the contents for all messages tagged with name
def get_all(self, name, clear=False): def get_all(self, name, clear=False):
self.lock.acquire()
if name in self.__messages and len(self.__messages[name])>0: if name in self.__messages and len(self.__messages[name])>0:
contents = self.__messages[name] contents = self.__messages[name]
if clear: if clear:
del self.__messages[name] del self.__messages[name]
self.lock.release()
return contents return contents
else:
self.lock.release()
return None
# deletes all messages tagged with name # deletes all messages tagged with name
def clear(self, name): def clear(self, name):
self.lock.acquire()
if name in self.__messages: if name in self.__messages:
del self.__messages[name] del self.__messages[name]
self.lock.release()
def __del__(self): def __del__(self):
if len(self.__messages)>0: if len(self.__messages)>0: