py-adtools 0.1.3__py3-none-any.whl → 0.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of py-adtools might be problematic. Click here for more details.
- adtools/evaluator.py +11 -11
- adtools/evaluator_pool.py +8 -8
- {py_adtools-0.1.3.dist-info → py_adtools-0.1.4.dist-info}/METADATA +1 -1
- py_adtools-0.1.4.dist-info/RECORD +10 -0
- py_adtools-0.1.3.dist-info/RECORD +0 -10
- {py_adtools-0.1.3.dist-info → py_adtools-0.1.4.dist-info}/WHEEL +0 -0
- {py_adtools-0.1.3.dist-info → py_adtools-0.1.4.dist-info}/licenses/LICENSE +0 -0
- {py_adtools-0.1.3.dist-info → py_adtools-0.1.4.dist-info}/top_level.txt +0 -0
adtools/evaluator.py
CHANGED
|
@@ -35,9 +35,9 @@ class PyEvaluator(ABC):
|
|
|
35
35
|
debug_mode: Debug mode.
|
|
36
36
|
join_timeout_seconds: Timeout in seconds to wait for the process to finish. Kill the process if timeout.
|
|
37
37
|
"""
|
|
38
|
-
self.
|
|
39
|
-
self.
|
|
40
|
-
self.
|
|
38
|
+
self.debug_mode = debug_mode
|
|
39
|
+
self.exec_code = exec_code
|
|
40
|
+
self.join_timeout_seconds = join_timeout_seconds
|
|
41
41
|
|
|
42
42
|
@abstractmethod
|
|
43
43
|
def evaluate_program(
|
|
@@ -73,13 +73,13 @@ class PyEvaluator(ABC):
|
|
|
73
73
|
children_processes = []
|
|
74
74
|
# Terminate parent process
|
|
75
75
|
process.terminate()
|
|
76
|
-
process.join(timeout=self.
|
|
76
|
+
process.join(timeout=self.join_timeout_seconds)
|
|
77
77
|
if process.is_alive():
|
|
78
78
|
process.kill()
|
|
79
79
|
process.join()
|
|
80
80
|
# Kill all children processes
|
|
81
81
|
for child in children_processes:
|
|
82
|
-
if self.
|
|
82
|
+
if self.debug_mode:
|
|
83
83
|
print(f"Killing process {process.pid}'s children process {child.pid}")
|
|
84
84
|
child.terminate()
|
|
85
85
|
|
|
@@ -97,7 +97,7 @@ class PyEvaluator(ABC):
|
|
|
97
97
|
class_names = [c.name for c in program.classes]
|
|
98
98
|
|
|
99
99
|
# Execute the code and get callable instances
|
|
100
|
-
if self.
|
|
100
|
+
if self.exec_code:
|
|
101
101
|
all_globals_namespace = {}
|
|
102
102
|
# Execute the program, map func/var/class to global namespace
|
|
103
103
|
exec(str(program), all_globals_namespace)
|
|
@@ -123,7 +123,7 @@ class PyEvaluator(ABC):
|
|
|
123
123
|
)
|
|
124
124
|
return res
|
|
125
125
|
except Exception as e:
|
|
126
|
-
if self.
|
|
126
|
+
if self.debug_mode:
|
|
127
127
|
print(e)
|
|
128
128
|
return None
|
|
129
129
|
|
|
@@ -148,7 +148,7 @@ class PyEvaluator(ABC):
|
|
|
148
148
|
self,
|
|
149
149
|
program: str | PyProgram,
|
|
150
150
|
timeout_seconds: int | float = None,
|
|
151
|
-
redirect_to_devnull: bool =
|
|
151
|
+
redirect_to_devnull: bool = False,
|
|
152
152
|
multiprocessing_start_method: Literal['default', 'auto', 'fork', 'spawn'] = 'auto',
|
|
153
153
|
**kwargs
|
|
154
154
|
):
|
|
@@ -188,13 +188,13 @@ class PyEvaluator(ABC):
|
|
|
188
188
|
# After getting the result, terminate/kill the process
|
|
189
189
|
self._kill_process_and_its_children(process)
|
|
190
190
|
except Empty: # The queue is empty indicates a timeout
|
|
191
|
-
if self.
|
|
191
|
+
if self.debug_mode:
|
|
192
192
|
print(f'DEBUG: the evaluation time exceeds {timeout_seconds}s.')
|
|
193
193
|
# Terminate/kill all processes if timeout happens
|
|
194
194
|
self._kill_process_and_its_children(process)
|
|
195
195
|
result = None
|
|
196
196
|
except Exception as e:
|
|
197
|
-
if self.
|
|
197
|
+
if self.debug_mode:
|
|
198
198
|
print(f'DEBUG: evaluation failed with exception:\n{e}')
|
|
199
199
|
# Terminate/kill all processes if meet exceptions
|
|
200
200
|
self._kill_process_and_its_children(process)
|
|
@@ -206,6 +206,6 @@ class PyEvaluator(ABC):
|
|
|
206
206
|
self._kill_process_and_its_children(process)
|
|
207
207
|
return result
|
|
208
208
|
except Exception as e:
|
|
209
|
-
if self.
|
|
209
|
+
if self.debug_mode:
|
|
210
210
|
print(e)
|
|
211
211
|
return None
|
adtools/evaluator_pool.py
CHANGED
|
@@ -26,12 +26,12 @@ class EvaluatorExecutorPool:
|
|
|
26
26
|
max_workers: The maximum number of workers.
|
|
27
27
|
pool_type: Type of the executor pool.
|
|
28
28
|
"""
|
|
29
|
-
self.
|
|
30
|
-
self.
|
|
29
|
+
self.evaluator = evaluator
|
|
30
|
+
self.max_workers = max_workers
|
|
31
31
|
if pool_type == 'thread':
|
|
32
|
-
self.
|
|
32
|
+
self.pool = ThreadPoolExecutor(max_workers=self.max_workers)
|
|
33
33
|
else:
|
|
34
|
-
self.
|
|
34
|
+
self.pool = ProcessPoolExecutor(max_workers=self.max_workers)
|
|
35
35
|
|
|
36
36
|
def evaluate(self, program: str | PyProgram, return_time=True, **kwargs):
|
|
37
37
|
"""Evaluate program.
|
|
@@ -40,7 +40,7 @@ class EvaluatorExecutorPool:
|
|
|
40
40
|
**kwargs: additional keyword arguments to pass to 'evaluate_program'.
|
|
41
41
|
"""
|
|
42
42
|
start_time = time.time()
|
|
43
|
-
future = self.
|
|
43
|
+
future = self.pool.submit(self.evaluator.evaluate, program, **kwargs)
|
|
44
44
|
res = future.result()
|
|
45
45
|
duration = time.time() - start_time
|
|
46
46
|
if return_time:
|
|
@@ -52,7 +52,7 @@ class EvaluatorExecutorPool:
|
|
|
52
52
|
self,
|
|
53
53
|
program: str | PyProgram,
|
|
54
54
|
timeout_seconds: Optional[float],
|
|
55
|
-
redirect_to_devnull: bool =
|
|
55
|
+
redirect_to_devnull: bool = False,
|
|
56
56
|
multiprocessing_start_method: Literal['default', 'auto', 'fork', 'spawn'] = 'auto',
|
|
57
57
|
return_time=True,
|
|
58
58
|
**kwargs
|
|
@@ -66,8 +66,8 @@ class EvaluatorExecutorPool:
|
|
|
66
66
|
**kwargs: additional keyword arguments to pass to 'evaluate_program'.
|
|
67
67
|
"""
|
|
68
68
|
start_time = time.time()
|
|
69
|
-
future = self.
|
|
70
|
-
self.
|
|
69
|
+
future = self.pool.submit(
|
|
70
|
+
self.evaluator.secure_evaluate,
|
|
71
71
|
program,
|
|
72
72
|
timeout_seconds,
|
|
73
73
|
redirect_to_devnull,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
adtools/__init__.py,sha256=kbxntZFeCcURiIypNOdMWyeKPdlzRsWOB-K7z6HNCsc,150
|
|
2
|
+
adtools/evaluator.py,sha256=-mz131oIVaM4pySDs7yYCKYCcAmJZrjd-_HhyqrO0s4,9074
|
|
3
|
+
adtools/evaluator_pool.py,sha256=v_NZibN4VI3STVUZt6ARdyoB4Z061xAefZlH8lkWsjE,2972
|
|
4
|
+
adtools/lm_base.py,sha256=fXltOlJpxy_b4ByX7nSIGXcMnH71W81tuzLsRtq_7JE,15709
|
|
5
|
+
adtools/py_code.py,sha256=FZfkp-IZ4zpOjrWe6svKNJsQhVANaTTkE0l0mc4aMW8,14277
|
|
6
|
+
py_adtools-0.1.4.dist-info/licenses/LICENSE,sha256=E5GGyecx3y5h2gcEGQloF-rDY9wbaef5IHjRsvtFbt8,1065
|
|
7
|
+
py_adtools-0.1.4.dist-info/METADATA,sha256=L1iStOAy6EP4eSwMlF-XxqcDhtJRy2vq8aJaxJSRq_I,6364
|
|
8
|
+
py_adtools-0.1.4.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
|
|
9
|
+
py_adtools-0.1.4.dist-info/top_level.txt,sha256=X2kKzmJFDAKR2FWCij5pfMG9pVVjVUomyl4e-1VLXIk,8
|
|
10
|
+
py_adtools-0.1.4.dist-info/RECORD,,
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
adtools/__init__.py,sha256=kbxntZFeCcURiIypNOdMWyeKPdlzRsWOB-K7z6HNCsc,150
|
|
2
|
-
adtools/evaluator.py,sha256=YAvnN1w7gIMNMnRGQAxU3hriVjEkJYQTph_ADwL_x2A,9083
|
|
3
|
-
adtools/evaluator_pool.py,sha256=GdvZu7KpJi79bP6l7A3sXu0voNh9YUfh07GPAk_qVeI,2981
|
|
4
|
-
adtools/lm_base.py,sha256=fXltOlJpxy_b4ByX7nSIGXcMnH71W81tuzLsRtq_7JE,15709
|
|
5
|
-
adtools/py_code.py,sha256=FZfkp-IZ4zpOjrWe6svKNJsQhVANaTTkE0l0mc4aMW8,14277
|
|
6
|
-
py_adtools-0.1.3.dist-info/licenses/LICENSE,sha256=E5GGyecx3y5h2gcEGQloF-rDY9wbaef5IHjRsvtFbt8,1065
|
|
7
|
-
py_adtools-0.1.3.dist-info/METADATA,sha256=jiotXGFPphm27xC-RZnwefjkSZcvwOwr_7TbY3LRfHM,6364
|
|
8
|
-
py_adtools-0.1.3.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
|
|
9
|
-
py_adtools-0.1.3.dist-info/top_level.txt,sha256=X2kKzmJFDAKR2FWCij5pfMG9pVVjVUomyl4e-1VLXIk,8
|
|
10
|
-
py_adtools-0.1.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|