Snake case

merge-requests/30/head
Bob Mottram 2022-01-03 18:44:40 +00:00
parent d180ce40cb
commit 651c0d2069
1 changed files with 51 additions and 45 deletions

View File

@ -15,8 +15,8 @@ import datetime
class thread_with_trace(threading.Thread): class thread_with_trace(threading.Thread):
def __init__(self, *args, **keywords): def __init__(self, *args, **keywords):
self.startTime = datetime.datetime.utcnow() self.start_time = datetime.datetime.utcnow()
self.isStarted = False self.is_started = False
tries = 0 tries = 0
while tries < 3: while tries < 3:
try: try:
@ -42,7 +42,7 @@ class thread_with_trace(threading.Thread):
time.sleep(1) time.sleep(1)
tries += 1 tries += 1
# note that this is set True even if all tries failed # note that this is set True even if all tries failed
self.isStarted = True self.is_started = True
def __run(self): def __run(self):
sys.settrace(self.globaltrace) sys.settrace(self.globaltrace)
@ -51,90 +51,96 @@ class thread_with_trace(threading.Thread):
self.run = self.__run_backup self.run = self.__run_backup
except Exception as ex: except Exception as ex:
print('ERROR: threads.py/__run failed - ' + str(ex)) print('ERROR: threads.py/__run failed - ' + str(ex))
pass
def globaltrace(self, frame, event, arg): def globaltrace(self, frame, event, arg):
"""Trace the thread
"""
if event == 'call': if event == 'call':
return self.localtrace return self.localtrace
else: return None
return None
def localtrace(self, frame, event, arg): def localtrace(self, frame, event, arg):
"""Trace the thread
"""
if self.killed: if self.killed:
if event == 'line': if event == 'line':
raise SystemExit() raise SystemExit()
return self.localtrace return self.localtrace
def kill(self): def kill(self):
"""Kill the thread
"""
self.killed = True self.killed = True
def clone(self, fn): def clone(self, func):
return thread_with_trace(target=fn, """Create a clone
"""
return thread_with_trace(target=func,
args=self._args, args=self._args,
daemon=True) daemon=True)
def remove_dormant_threads(base_dir: str, threadsList: [], debug: bool, def remove_dormant_threads(base_dir: str, threads_list: [], debug: bool,
timeoutMins: int) -> None: timeout_mins: int) -> None:
"""Removes threads whose execution has completed """Removes threads whose execution has completed
""" """
if len(threadsList) == 0: if len(threads_list) == 0:
return return
timeoutSecs = int(timeoutMins * 60) timeout_secs = int(timeout_mins * 60)
dormantThreads = [] dormant_threads = []
curr_time = datetime.datetime.utcnow() curr_time = datetime.datetime.utcnow()
changed = False changed = False
# which threads are dormant? # which threads are dormant?
noOfActiveThreads = 0 no_of_active_threads = 0
for th in threadsList: for thrd in threads_list:
removeThread = False remove_thread = False
if th.isStarted: if thrd.is_started:
if not th.is_alive(): if not thrd.is_alive():
if (curr_time - th.startTime).total_seconds() > 10: if (curr_time - thrd.start_time).total_seconds() > 10:
if debug: if debug:
print('DEBUG: ' + print('DEBUG: ' +
'thread is not alive ten seconds after start') 'thread is not alive ten seconds after start')
removeThread = True remove_thread = True
# timeout for started threads # timeout for started threads
if (curr_time - th.startTime).total_seconds() > timeoutSecs: if (curr_time - thrd.start_time).total_seconds() > timeout_secs:
if debug: if debug:
print('DEBUG: started thread timed out') print('DEBUG: started thread timed out')
removeThread = True remove_thread = True
else: else:
# timeout for threads which havn't been started # timeout for threads which havn't been started
if (curr_time - th.startTime).total_seconds() > timeoutSecs: if (curr_time - thrd.start_time).total_seconds() > timeout_secs:
if debug: if debug:
print('DEBUG: unstarted thread timed out') print('DEBUG: unstarted thread timed out')
removeThread = True remove_thread = True
if removeThread: if remove_thread:
dormantThreads.append(th) dormant_threads.append(thrd)
else: else:
noOfActiveThreads += 1 no_of_active_threads += 1
if debug: if debug:
print('DEBUG: ' + str(noOfActiveThreads) + print('DEBUG: ' + str(no_of_active_threads) +
' active threads out of ' + str(len(threadsList))) ' active threads out of ' + str(len(threads_list)))
# remove the dormant threads # remove the dormant threads
dormantCtr = 0 dormant_ctr = 0
for th in dormantThreads: for thrd in dormant_threads:
if debug: if debug:
print('DEBUG: Removing dormant thread ' + str(dormantCtr)) print('DEBUG: Removing dormant thread ' + str(dormant_ctr))
dormantCtr += 1 dormant_ctr += 1
threadsList.remove(th) threads_list.remove(thrd)
th.kill() thrd.kill()
changed = True changed = True
# start scheduled threads # start scheduled threads
if len(threadsList) < 10: if len(threads_list) < 10:
ctr = 0 ctr = 0
for th in threadsList: for thrd in threads_list:
if not th.isStarted: if not thrd.is_started:
print('Starting new send thread ' + str(ctr)) print('Starting new send thread ' + str(ctr))
th.start() thrd.start()
changed = True changed = True
break break
ctr += 1 ctr += 1
@ -143,12 +149,12 @@ def remove_dormant_threads(base_dir: str, threadsList: [], debug: bool,
return return
if debug: if debug:
sendLogFilename = base_dir + '/send.csv' send_log_filename = base_dir + '/send.csv'
try: try:
with open(sendLogFilename, 'a+') as logFile: with open(send_log_filename, 'a+') as fp_log:
logFile.write(curr_time.strftime("%Y-%m-%dT%H:%M:%SZ") + fp_log.write(curr_time.strftime("%Y-%m-%dT%H:%M:%SZ") +
',' + str(noOfActiveThreads) + ',' + str(no_of_active_threads) +
',' + str(len(threadsList)) + '\n') ',' + str(len(threads_list)) + '\n')
except OSError: except OSError:
print('EX: remove_dormant_threads unable to write ' + print('EX: remove_dormant_threads unable to write ' +
sendLogFilename) send_log_filename)