2019-06-30 16:36:58 +00:00
|
|
|
__filename__ = "threads.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2019-12-14 10:52:19 +00:00
|
|
|
__version__ = "1.1.0"
|
2019-06-30 16:36:58 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
|
|
|
|
|
|
|
import threading
|
2020-02-19 12:27:21 +00:00
|
|
|
import os
|
2019-06-30 16:36:58 +00:00
|
|
|
import sys
|
|
|
|
import trace
|
2019-06-30 16:50:43 +00:00
|
|
|
import time
|
2019-10-16 15:17:00 +00:00
|
|
|
import datetime
|
2019-06-30 16:36:58 +00:00
|
|
|
|
2019-10-16 15:17:00 +00:00
|
|
|
class threadWithTrace(threading.Thread):
|
2019-09-03 10:24:15 +00:00
|
|
|
def __init__(self, *args, **keywords):
|
2019-10-16 16:03:45 +00:00
|
|
|
self.startTime=datetime.datetime.utcnow()
|
2019-10-16 18:19:18 +00:00
|
|
|
self.isStarted=False
|
2019-10-14 20:32:00 +00:00
|
|
|
tries=0
|
|
|
|
while tries<3:
|
|
|
|
try:
|
|
|
|
self._args, self._keywords = args, keywords
|
|
|
|
threading.Thread.__init__(self, *self._args, **self._keywords)
|
|
|
|
self.killed = False
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print('ERROR: threads.py/__init__ failed - '+str(e))
|
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-06-30 16:36:58 +00:00
|
|
|
|
|
|
|
def start(self):
|
2019-10-14 20:32:00 +00:00
|
|
|
tries=0
|
|
|
|
while tries<3:
|
|
|
|
try:
|
|
|
|
self.__run_backup = self.run
|
|
|
|
self.run = self.__run
|
|
|
|
threading.Thread.start(self)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print('ERROR: threads.py/start failed - '+str(e))
|
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-10-16 18:19:18 +00:00
|
|
|
# note that this is set True even if all tries failed
|
|
|
|
self.isStarted=True
|
|
|
|
|
2019-10-14 20:32:00 +00:00
|
|
|
def __run(self):
|
2019-10-19 18:08:47 +00:00
|
|
|
sys.settrace(self.globaltrace)
|
|
|
|
self.__run_backup()
|
|
|
|
self.run = self.__run_backup
|
2019-10-14 20:32:00 +00:00
|
|
|
|
2019-06-30 16:36:58 +00:00
|
|
|
def globaltrace(self, frame, event, arg):
|
|
|
|
if event == 'call':
|
|
|
|
return self.localtrace
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def localtrace(self, frame, event, arg):
|
|
|
|
if self.killed:
|
|
|
|
if event == 'line':
|
|
|
|
raise SystemExit()
|
|
|
|
return self.localtrace
|
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
self.killed = True
|
2019-09-03 10:24:15 +00:00
|
|
|
|
2019-09-03 11:10:53 +00:00
|
|
|
def clone(self,fn):
|
|
|
|
return threadWithTrace(target=fn, \
|
|
|
|
args=self._args, \
|
|
|
|
daemon=True)
|
2019-10-16 10:08:21 +00:00
|
|
|
|
2019-10-16 18:19:18 +00:00
|
|
|
def removeDormantThreads(baseDir: str,threadsList: [],debug: bool) -> None:
|
2019-10-16 10:08:21 +00:00
|
|
|
"""Removes threads whose execution has completed
|
|
|
|
"""
|
|
|
|
if len(threadsList)==0:
|
|
|
|
return
|
|
|
|
|
|
|
|
dormantThreads=[]
|
2019-10-16 15:17:00 +00:00
|
|
|
currTime=datetime.datetime.utcnow()
|
2019-10-16 18:19:18 +00:00
|
|
|
changed=False
|
2019-10-16 10:08:21 +00:00
|
|
|
|
|
|
|
# which threads are dormant?
|
|
|
|
noOfActiveThreads=0
|
|
|
|
for th in threadsList:
|
2019-10-16 15:17:00 +00:00
|
|
|
removeThread=False
|
|
|
|
|
2019-10-16 18:19:18 +00:00
|
|
|
if th.isStarted:
|
|
|
|
if not th.is_alive():
|
2019-10-23 10:30:11 +00:00
|
|
|
if (currTime-th.startTime).total_seconds()>10:
|
2019-10-23 10:23:43 +00:00
|
|
|
if debug:
|
2019-10-23 10:30:11 +00:00
|
|
|
print('DEBUG: thread is not alive ten seconds after start')
|
2019-10-23 10:23:43 +00:00
|
|
|
removeThread=True
|
2019-10-23 10:30:11 +00:00
|
|
|
# timeout for started threads
|
2019-11-07 21:03:17 +00:00
|
|
|
if (currTime-th.startTime).total_seconds()>600:
|
2019-10-23 10:30:11 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: started thread timed out')
|
|
|
|
removeThread=True
|
2019-10-23 10:21:41 +00:00
|
|
|
else:
|
|
|
|
# timeout for threads which havn't been started
|
2019-10-23 10:30:11 +00:00
|
|
|
if (currTime-th.startTime).total_seconds()>600:
|
2019-10-23 10:21:41 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: unstarted thread timed out')
|
2019-10-16 18:19:18 +00:00
|
|
|
removeThread=True
|
2019-10-16 15:17:00 +00:00
|
|
|
|
|
|
|
if removeThread:
|
2019-10-16 10:08:21 +00:00
|
|
|
dormantThreads.append(th)
|
|
|
|
else:
|
|
|
|
noOfActiveThreads+=1
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: '+str(noOfActiveThreads) + ' active threads out of '+str(len(threadsList)))
|
|
|
|
|
|
|
|
# remove the dormant threads
|
|
|
|
dormantCtr=0
|
|
|
|
for th in dormantThreads:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: Removing dormant thread '+str(dormantCtr))
|
2019-10-16 15:17:00 +00:00
|
|
|
dormantCtr+=1
|
|
|
|
threadsList.remove(th)
|
|
|
|
th.kill()
|
2019-10-16 18:19:18 +00:00
|
|
|
changed=True
|
|
|
|
|
|
|
|
# start scheduled threads
|
|
|
|
if len(threadsList)<10:
|
|
|
|
ctr=0
|
|
|
|
for th in threadsList:
|
|
|
|
if not th.isStarted:
|
|
|
|
print('Starting new send thread '+str(ctr))
|
|
|
|
th.start()
|
|
|
|
changed=True
|
|
|
|
break
|
|
|
|
ctr+=1
|
|
|
|
|
|
|
|
if not changed:
|
|
|
|
return
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
sendLogFilename=baseDir+'/send.csv'
|
2020-02-19 12:27:21 +00:00
|
|
|
try:
|
|
|
|
with open(sendLogFilename, "a+") as logFile:
|
|
|
|
logFile.write(currTime.strftime("%Y-%m-%dT%H:%M:%SZ")+','+str(noOfActiveThreads)+','+str(len(threadsList))+'\n')
|
|
|
|
except:
|
|
|
|
pass
|