From c8f9825e6b3e507a0c27a2dbc917274532e8f89b Mon Sep 17 00:00:00 2001 From: Nyo Date: Mon, 6 Jun 2016 12:39:24 +0200 Subject: [PATCH] .BANCHO. Thread safe log to file --- helpers/logHelper.py | 10 +++++++--- objects/fileLocks.py | 20 ++++++++++++++++++++ objects/glob.py | 2 ++ objects/logThread.py | 24 ++++++++++++++++++++++++ objects/match.py | 2 -- 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 objects/fileLocks.py create mode 100644 objects/logThread.py diff --git a/helpers/logHelper.py b/helpers/logHelper.py index baa0447..1531818 100644 --- a/helpers/logHelper.py +++ b/helpers/logHelper.py @@ -5,6 +5,7 @@ from helpers.systemHelper import runningUnderUnix from objects import glob ENDL = "\n" if runningUnderUnix() else "\r\n" + def logMessage(message, alertType = "INFO", messageColor = bcolors.ENDC, discord = False, alertDev = False, of = None, stdout = True): """ Logs a message to stdout/discord/file @@ -52,9 +53,12 @@ def logMessage(message, alertType = "INFO", messageColor = bcolors.ENDC, discord # Log to file if needed if of != None: - # TODO: Lock - with open(".data/{}".format(of), "a") as f: - f.write(finalMessage+ENDL) + try: + glob.fLocks.lockFile(of) + with open(".data/{}".format(of), "a") as f: + f.write(finalMessage+ENDL) + finally: + glob.fLocks.unlockFile(of) def warning(message, discord = False, alertDev = False): """ diff --git a/objects/fileLocks.py b/objects/fileLocks.py new file mode 100644 index 0000000..38f90ff --- /dev/null +++ b/objects/fileLocks.py @@ -0,0 +1,20 @@ +import threading + +class fileLocks: + def __init__(self): + # Dictionary containing threading.Lock s + self.locks = {} + + def lockFile(self, fileName): + if fileName in self.locks: + # Acquire existing lock + self.locks[fileName].acquire() + else: + # Create new lock and acquire it + self.locks[fileName] = threading.Lock() + self.locks[fileName].acquire() + + def unlockFile(self, fileName): + if fileName in self.locks: + # Release lock if it exists + self.locks[fileName].release() diff --git a/objects/glob.py b/objects/glob.py index 02f56e0..523aa69 100644 --- a/objects/glob.py +++ b/objects/glob.py @@ -3,6 +3,7 @@ from objects import tokenList from objects import channelList from objects import matchList +from objects import fileLocks VERSION = "1.2" @@ -14,6 +15,7 @@ channels = channelList.channelList() matches = matchList.matchList() restarting = False pool = None +fLocks = fileLocks.fileLocks() debug = False diff --git a/objects/logThread.py b/objects/logThread.py new file mode 100644 index 0000000..299dd9c --- /dev/null +++ b/objects/logThread.py @@ -0,0 +1,24 @@ +''' +import threading + +class task: + def __init__(self, function, args = (), kwargs = {}): + self.function = function + self.args = args + self.kwargs = kwargs + +class logThread: + def __init__(self): + self.thread = threading.Thread() + self.queue = [] + + def enqueue(self, function, args = (), kwargs = {}): + self.queue.append(task(function, args, kwargs)) + + def run(self): + for i in self.queue: + self.thread = threading.Thread(i.function, i.args, i.kwargs) + self.thread.run() + self.thread.join() + self.queue = [] +''' diff --git a/objects/match.py b/objects/match.py index 242be59..b3d7ec8 100644 --- a/objects/match.py +++ b/objects/match.py @@ -5,8 +5,6 @@ from constants import matchTeamTypes from constants import matchModModes from constants import slotStatuses from objects import glob -from helpers import consoleHelper -from constants import bcolors from constants import serverPackets from constants import dataTypes from constants import matchTeams