From 28579670b99670ec31e0882cf4aa7114e469e98a Mon Sep 17 00:00:00 2001 From: Zhan Ling Date: Wed, 10 Apr 2024 11:40:52 +0800 Subject: [PATCH 1/3] Modify the time_eval func in utils.py for killing timeout sub-process --- hcga/utils.py | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/hcga/utils.py b/hcga/utils.py index a83e80f2..d2efb540 100644 --- a/hcga/utils.py +++ b/hcga/utils.py @@ -37,14 +37,40 @@ class NestedPool(multiprocessing.pool.Pool): # pylint: disable=abstract-method def timeout_eval(func, args, timeout=None, pool=None): - """Evaluate a function and kill it is it takes longer than timeout. - - If timeout is Nonei or == 0, a simple evaluation will take place. + """Evaluate a function within a given timeout period. + + Args: + func: The function to call. + args: Arguments to pass to the function. + timeout: The timeout period in seconds. + + Returns: + The function's result, or None if a timeout or an error occurs. """ if timeout is None or timeout == 0: - return func(*args) - - return pool.apply_async(func, args).get(timeout=timeout) + try: + return func(*args) + except Exception: + return None + + def target(queue, args): + try: + result = func(*args) + queue.put(result) + except Exception: + queue.put(None) + + queue = multiprocessing.Queue() + process = multiprocessing.Process(target=target, args=(queue, args)) + process.start() + process.join(timeout) + + if process.is_alive(): + process.terminate() + process.join() + return None + + return queue.get_nowait() def get_trivial_graph(n_node_features=0): From b71dd320eced9fa66f20f5eaf81ea354199eef4b Mon Sep 17 00:00:00 2001 From: Zhan Ling Date: Wed, 10 Apr 2024 15:09:28 +0800 Subject: [PATCH 2/3] Fix whitespace issues --- hcga/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hcga/utils.py b/hcga/utils.py index d2efb540..7c0b725c 100644 --- a/hcga/utils.py +++ b/hcga/utils.py @@ -38,12 +38,12 @@ class NestedPool(multiprocessing.pool.Pool): # pylint: disable=abstract-method def timeout_eval(func, args, timeout=None, pool=None): """Evaluate a function within a given timeout period. - + Args: func: The function to call. args: Arguments to pass to the function. timeout: The timeout period in seconds. - + Returns: The function's result, or None if a timeout or an error occurs. """ From 69f5fab8f83db80a147e0802d3a0ffd5f21e60d3 Mon Sep 17 00:00:00 2001 From: arnaudon Date: Mon, 15 Apr 2024 12:08:38 +0200 Subject: [PATCH 3/3] fix ci --- hcga/feature_class.py | 5 +---- hcga/utils.py | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/hcga/feature_class.py b/hcga/feature_class.py index 9bae8df6..2674a6a2 100644 --- a/hcga/feature_class.py +++ b/hcga/feature_class.py @@ -235,16 +235,13 @@ def evaluate_feature( # pylint: disable=too-many-branches try: try: - feature = timeout_eval( - feature_function, (function_args,), timeout=self.timeout, pool=self.pool - ) + feature = timeout_eval(feature_function, (function_args,), timeout=self.timeout) except NetworkXNotImplemented: if self.graph_type == "directed": feature = timeout_eval( feature_function, (to_undirected(function_args),), timeout=self.timeout, - pool=self.pool, ) else: return None diff --git a/hcga/utils.py b/hcga/utils.py index 7c0b725c..3bd19e43 100644 --- a/hcga/utils.py +++ b/hcga/utils.py @@ -36,7 +36,7 @@ class NestedPool(multiprocessing.pool.Pool): # pylint: disable=abstract-method Process = NoDaemonProcess -def timeout_eval(func, args, timeout=None, pool=None): +def timeout_eval(func, args, timeout=None): """Evaluate a function within a given timeout period. Args: @@ -50,14 +50,14 @@ def timeout_eval(func, args, timeout=None, pool=None): if timeout is None or timeout == 0: try: return func(*args) - except Exception: + except Exception: # pylint: disable=broad-exception-caught return None def target(queue, args): try: result = func(*args) queue.put(result) - except Exception: + except Exception: # pylint: disable=broad-exception-caught queue.put(None) queue = multiprocessing.Queue()