mirror of
https://github.com/iio612/DEFENDER.git
synced 2026-02-13 11:14:23 +00:00
Trigger a thread clean-up before running a new thread
This commit is contained in:
15
core/base.py
15
core/base.py
@@ -324,7 +324,7 @@ class Base:
|
|||||||
self.logs.error(f'Assertion Error -> {ae}')
|
self.logs.error(f'Assertion Error -> {ae}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def create_thread(self, func:object, func_args: tuple = (), run_once:bool = False, daemon: bool = True) -> None:
|
def create_thread(self, func: object, func_args: tuple = (), run_once: bool = False, daemon: bool = True) -> None:
|
||||||
"""Create a new thread and store it into running_threads variable
|
"""Create a new thread and store it into running_threads variable
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -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:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user