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