Removing dormant threads

main2
Bob Mottram 2019-10-16 11:08:21 +01:00
parent 6ea855feef
commit 49d252c0d1
2 changed files with 31 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import time
from time import gmtime, strftime
from collections import OrderedDict
from threads import threadWithTrace
from threads import removeDormantThreads
from cache import storePersonInCache
from cache import getPersonFromCache
from cache import expirePersonCache
@ -1042,7 +1043,7 @@ def threadSendPost(session,postJsonStr: str,federationList: [],\
time.sleep(backoffTime)
backoffTime *= 2
tries+=1
def sendPost(projectVersion: str, \
session,baseDir: str,nickname: str, domain: str, port: int, \
toNickname: str, toDomain: str, toPort: int, cc: str, \
@ -1128,8 +1129,10 @@ def sendPost(projectVersion: str, \
toDomain,toPort, \
postPath,httpPrefix,withDigest,postJsonStr)
removeDormantThreads(sendThreads,debug)
# Keep the number of threads being used small
while len(sendThreads)>10:
while len(sendThreads)>20:
sendThreads[0].kill()
sendThreads.pop(0)
thr = threadWithTrace(target=threadSendPost,args=(session, \

View File

@ -70,3 +70,29 @@ class threadWithTrace(threading.Thread):
return threadWithTrace(target=fn, \
args=self._args, \
daemon=True)
def removeDormantThreads(threadsList: [],debug: bool) -> None:
"""Removes threads whose execution has completed
"""
if len(threadsList)==0:
return
dormantThreads=[]
# which threads are dormant?
noOfActiveThreads=0
for th in threadsList:
if not th.is_alive():
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))
dormantCtr+=1
threadsList.remove(th)