Trigger a thread clean-up before running a new thread

This commit is contained in:
adator
2025-11-16 13:15:49 +01:00
parent 6a0d4e2286
commit 7ffc58d4ff

View File

@@ -333,6 +333,9 @@ class Base:
run_once (bool, optional): If you want to ensure that this method/function run once. Defaults to False. run_once (bool, optional): If you want to ensure that this method/function run once. Defaults to False.
""" """
try: try:
# Clean unused threads first
self.garbage_collector_thread()
func_name = func.__name__ func_name = func.__name__
if run_once: if run_once:
@@ -346,8 +349,8 @@ class Base:
self.running_threads.append(th) self.running_threads.append(th)
self.logs.debug(f"-- Thread ID : {str(th.ident)} | Thread name : {th.name} | Running Threads : {len(threading.enumerate())}") self.logs.debug(f"-- Thread ID : {str(th.ident)} | Thread name : {th.name} | Running Threads : {len(threading.enumerate())}")
except AssertionError as ae: except Exception as err:
self.logs.error(f'{ae}') self.logs.error(err, exc_info=True)
def is_thread_alive(self, thread_name: str) -> bool: def is_thread_alive(self, thread_name: str) -> bool:
"""Check if the thread is still running! using the is_alive method of Threads. """Check if the thread is still running! using the is_alive method of Threads.
@@ -424,11 +427,11 @@ class Base:
if thread.name != 'heartbeat': if thread.name != 'heartbeat':
if not thread.is_alive(): if not thread.is_alive():
self.running_threads.remove(thread) self.running_threads.remove(thread)
self.logs.info(f"-- Thread {str(thread.name)} {str(thread.native_id)} removed") self.logs.debug(f"-- Thread {str(thread.name)} {str(thread.native_id)} has been removed!")
# print(threading.enumerate()) # print(threading.enumerate())
except AssertionError as ae: except Exception as err:
self.logs.error(f'Assertion Error -> {ae}') self.logs.error(err, exc_info=True)
def garbage_collector_sockets(self) -> None: def garbage_collector_sockets(self) -> None: