flake8 format

main
Bob Mottram 2020-04-04 13:09:57 +01:00
parent 02ce4e3d14
commit ad2c26eebf
1 changed files with 62 additions and 58 deletions

View File

@ -1,53 +1,52 @@
__filename__="threads.py" __filename__ = "threads.py"
__author__="Bob Mottram" __author__ = "Bob Mottram"
__license__="AGPL3+" __license__ = "AGPL3+"
__version__="1.1.0" __version__ = "1.1.0"
__maintainer__="Bob Mottram" __maintainer__ = "Bob Mottram"
__email__="bob@freedombone.net" __email__ = "bob@freedombone.net"
__status__="Production" __status__ = "Production"
import threading import threading
import os
import sys import sys
import trace
import time import time
import datetime import datetime
class threadWithTrace(threading.Thread): class threadWithTrace(threading.Thread):
def __init__(self, *args, **keywords): def __init__(self, *args, **keywords):
self.startTime=datetime.datetime.utcnow() self.startTime = datetime.datetime.utcnow()
self.isStarted=False self.isStarted = False
tries=0 tries = 0
while tries<3: while tries < 3:
try: try:
self._args,self._keywords=args,keywords self._args, self._keywords = args, keywords
threading.Thread.__init__(self,*self._args,**self._keywords) threading.Thread.__init__(self, *self._args, **self._keywords)
self.killed=False self.killed = False
break break
except Exception as e: except Exception as e:
print('ERROR: threads.py/__init__ failed - '+str(e)) print('ERROR: threads.py/__init__ failed - ' + str(e))
time.sleep(1) time.sleep(1)
tries+=1 tries += 1
def start(self): def start(self):
tries=0 tries = 0
while tries<3: while tries < 3:
try: try:
self.__run_backup=self.run self.__run_backup = self.run
self.run=self.__run self.run = self.__run
threading.Thread.start(self) threading.Thread.start(self)
break break
except Exception as e: except Exception as e:
print('ERROR: threads.py/start failed - '+str(e)) print('ERROR: threads.py/start failed - ' + str(e))
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.isStarted = True
def __run(self): def __run(self):
sys.settrace(self.globaltrace) sys.settrace(self.globaltrace)
self.__run_backup() self.__run_backup()
self.run=self.__run_backup self.run = self.__run_backup
def globaltrace(self, frame, event, arg): def globaltrace(self, frame, event, arg):
if event == 'call': if event == 'call':
@ -62,81 +61,86 @@ class threadWithTrace(threading.Thread):
return self.localtrace return self.localtrace
def kill(self): def kill(self):
self.killed=True self.killed = True
def clone(self,fn): def clone(self, fn):
return threadWithTrace(target=fn, \ return threadWithTrace(target=fn,
args=self._args, \ args=self._args,
daemon=True) daemon=True)
def removeDormantThreads(baseDir: str,threadsList: [],debug: bool) -> None:
def removeDormantThreads(baseDir: str, threadsList: [], debug: bool) -> None:
"""Removes threads whose execution has completed """Removes threads whose execution has completed
""" """
if len(threadsList)==0: if len(threadsList) == 0:
return return
dormantThreads=[] dormantThreads = []
currTime=datetime.datetime.utcnow() currTime = datetime.datetime.utcnow()
changed=False changed = False
# which threads are dormant? # which threads are dormant?
noOfActiveThreads=0 noOfActiveThreads = 0
for th in threadsList: for th in threadsList:
removeThread=False removeThread = False
if th.isStarted: if th.isStarted:
if not th.is_alive(): if not th.is_alive():
if (currTime-th.startTime).total_seconds()>10: if (currTime - th.startTime).total_seconds() > 10:
if debug: if debug:
print('DEBUG: thread is not alive ten seconds after start') print('DEBUG: ' +
removeThread=True 'thread is not alive ten seconds after start')
removeThread = True
# timeout for started threads # timeout for started threads
if (currTime-th.startTime).total_seconds()>600: if (currTime - th.startTime).total_seconds() > 600:
if debug: if debug:
print('DEBUG: started thread timed out') print('DEBUG: started thread timed out')
removeThread=True removeThread = True
else: else:
# timeout for threads which havn't been started # timeout for threads which havn't been started
if (currTime-th.startTime).total_seconds()>600: if (currTime - th.startTime).total_seconds() > 600:
if debug: if debug:
print('DEBUG: unstarted thread timed out') print('DEBUG: unstarted thread timed out')
removeThread=True removeThread = True
if removeThread: if removeThread:
dormantThreads.append(th) dormantThreads.append(th)
else: else:
noOfActiveThreads+=1 noOfActiveThreads += 1
if debug: if debug:
print('DEBUG: '+str(noOfActiveThreads) + ' active threads out of '+str(len(threadsList))) print('DEBUG: ' + str(noOfActiveThreads) +
' active threads out of ' + str(len(threadsList)))
# remove the dormant threads # remove the dormant threads
dormantCtr=0 dormantCtr = 0
for th in dormantThreads: for th in dormantThreads:
if debug: if debug:
print('DEBUG: Removing dormant thread '+str(dormantCtr)) print('DEBUG: Removing dormant thread ' + str(dormantCtr))
dormantCtr+=1 dormantCtr += 1
threadsList.remove(th) threadsList.remove(th)
th.kill() th.kill()
changed=True changed = True
# start scheduled threads # start scheduled threads
if len(threadsList)<10: if len(threadsList) < 10:
ctr=0 ctr = 0
for th in threadsList: for th in threadsList:
if not th.isStarted: if not th.isStarted:
print('Starting new send thread '+str(ctr)) print('Starting new send thread ' + str(ctr))
th.start() th.start()
changed=True changed = True
break break
ctr+=1 ctr += 1
if not changed: if not changed:
return return
if debug: if debug:
sendLogFilename=baseDir+'/send.csv' sendLogFilename = baseDir + '/send.csv'
try: try:
with open(sendLogFilename, "a+") as logFile: with open(sendLogFilename, "a+") as logFile:
logFile.write(currTime.strftime("%Y-%m-%dT%H:%M:%SZ")+','+str(noOfActiveThreads)+','+str(len(threadsList))+'\n') logFile.write(currTime.strftime("%Y-%m-%dT%H:%M:%SZ") +
except: ',' + str(noOfActiveThreads) +
',' + str(len(threadsList)) + '\n')
except BaseException:
pass pass