(code_content: str, data_directory: Path, timeout_seconds: int = 120)
| 405 | |
| 406 | |
| 407 | def execute_code_with_timeout(code_content: str, data_directory: Path, timeout_seconds: int = 120): |
| 408 | |
| 409 | result_queue = Queue() |
| 410 | |
| 411 | process = Process(target=execute_code_in_process, |
| 412 | args=(code_content, data_directory, result_queue, timeout_seconds)) |
| 413 | |
| 414 | process.start() |
| 415 | process.join(timeout=timeout_seconds) |
| 416 | |
| 417 | if process.is_alive(): |
| 418 | process.terminate() |
| 419 | process.join(timeout=5) |
| 420 | |
| 421 | if process.is_alive(): |
| 422 | process.kill() |
| 423 | process.join() |
| 424 | |
| 425 | raise ExecutionTimeoutError(f"Code execution timeout (exceeded {timeout_seconds} seconds)") |
| 426 | |
| 427 | if not result_queue.empty(): |
| 428 | result_type, result_message = result_queue.get() |
| 429 | if result_type == 'error': |
| 430 | raise Exception(result_message) |
| 431 | else: |
| 432 | raise Exception("Code execution terminated abnormally") |
| 433 | |
| 434 | |
| 435 | def execute_and_save_plot(code: str, task_info: Dict, results_dir: Path, timeout: int) -> Tuple[bool, str, Optional[Path]]: |
no test coverage detected