|
16 | 16 | import importlib |
17 | 17 | import typing |
18 | 18 | from typing import Any, ClassVar, Dict, List, Optional |
19 | | - |
| 19 | +import concurrent.futures |
20 | 20 | from camel.interpreters.base import BaseInterpreter |
21 | 21 | from camel.interpreters.interpreter_error import InterpreterError |
22 | 22 |
|
@@ -87,15 +87,17 @@ def __init__( |
87 | 87 | import_white_list: Optional[List[str]] = None, |
88 | 88 | unsafe_mode: bool = False, |
89 | 89 | raise_error: bool = False, |
| 90 | + default_timeout:int = 60 |
90 | 91 | ) -> None: |
91 | 92 | self.action_space = action_space or dict() |
92 | 93 | self.state = self.action_space.copy() |
93 | 94 | self.fuzz_state: Dict[str, Any] = dict() |
94 | 95 | self.import_white_list = import_white_list or list() |
95 | 96 | self.raise_error = raise_error |
96 | 97 | self.unsafe_mode = unsafe_mode |
| 98 | + self.default_timeout = default_timeout |
97 | 99 |
|
98 | | - def run(self, code: str, code_type: str) -> str: |
| 100 | + def run(self, code: str, code_type: str, timeout:int =None) -> str: |
99 | 101 | r"""Executes the given code with specified code type in the |
100 | 102 | interpreter. |
101 | 103 |
|
@@ -145,7 +147,13 @@ def run(self, code: str, code_type: str) -> str: |
145 | 147 |
|
146 | 148 | return result |
147 | 149 | else: |
148 | | - return str(self.execute(code)) |
| 150 | + try: |
| 151 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 152 | + future = executor.submit(self._execute_ast) |
| 153 | + line_result = future.result(timeout=timeout or self.default_timeout) |
| 154 | + return str(line_result) |
| 155 | + except concurrent.futures.TimeoutError: |
| 156 | + raise InterpreterError("Code execution timed out.") |
149 | 157 |
|
150 | 158 | def update_action_space(self, action_space: Dict[str, Any]) -> None: |
151 | 159 | r"""Updates action space for *python* interpreter.""" |
|
0 commit comments