diff --git a/posts.py b/posts.py index 42fc8420f..828ea42bd 100644 --- a/posts.py +++ b/posts.py @@ -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, \ diff --git a/threads.py b/threads.py index b147777b3..9a41955be 100644 --- a/threads.py +++ b/threads.py @@ -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)