(code_content: str, data_directory: Path, timeout_seconds: int = 120)
| 373 | |
| 374 | |
| 375 | def execute_code_with_timeout(code_content: str, data_directory: Path, timeout_seconds: int = 120): |
| 376 | |
| 377 | result_queue = Queue() |
| 378 | |
| 379 | process = Process(target=execute_code_in_process, |
| 380 | args=(code_content, data_directory, result_queue, timeout_seconds)) |
| 381 | |
| 382 | process.start() |
| 383 | process.join(timeout=timeout_seconds) |
| 384 | |
| 385 | if process.is_alive(): |
| 386 | process.terminate() |
| 387 | process.join(timeout=5) |
| 388 | |
| 389 | if process.is_alive(): |
| 390 | process.kill() |
| 391 | process.join() |
| 392 | |
| 393 | raise ExecutionTimeoutError(f"Code execution timeout (exceeded {timeout_seconds} seconds)") |
| 394 | |
| 395 | if not result_queue.empty(): |
| 396 | result_type, result_message = result_queue.get() |
| 397 | if result_type == 'error': |
| 398 | raise Exception(result_message) |
| 399 | else: |
| 400 | raise Exception("Code execution terminated abnormally") |
| 401 | |
| 402 | |
| 403 | def execute_and_save_plot(code: str, task_info: Dict, results_dir: Path, timeout: int) -> Tuple[bool, str, Optional[Path]]: |
no test coverage detected