(no title)
imaginaryspaces | 1 year ago
To set some context: The code handles isolated execution of machine learning code.
Original version:
def run_code(code, timeout=3600):
try:
p = Process(target=exec, args=(code,))
p.start()
p.join(timeout)
if p.is_alive():
p.terminate()
return "Timeout"
return "Success"
except Exception as e:
return f"Failed: {str(e)}"
New version: class ProcessExecutor(Executor):
def __init__(self, execution_id: str, code: str,
working_dir: Path, dataset: pd.DataFrame,
timeout: int = 3600):
super().__init__(code, timeout)
self.working_dir = Path(working_dir).resolve() / execution_id
self.working_dir.mkdir(parents=True, exist_ok=True)
self.dataset = dataset
def run(self) -> ExecutionResult:
start_time = time.time()
code_file: Path = self.working_dir / self.code_file_name
with open(code_file, "w") as f:
f.write(self.code)
try:
process = subprocess.Popen(
[sys.executable, str(code_file)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=str(self.working_dir),
text=True,
)
stdout, stderr = process.communicate(timeout=self.timeout)
return ExecutionResult(
term_out=[stdout],
exec_time=time.time() - start_time,
model_artifacts=self._collect_artifacts(),
performance=extract_performance(stdout),
)
except subprocess.TimeoutExpired:
process.kill()
return ExecutionResult(
term_out=[],
exec_time=self.timeout,
exception=TimeoutError(
f"Execution exceeded {self.timeout}s timeout"
),
)
sophiebits|1 year ago
waonderer|1 year ago
imaginaryspaces|1 year ago
android521|1 year ago
imaginaryspaces|1 year ago
dakiol|1 year ago
imaginaryspaces|1 year ago
zerr|1 year ago
imaginaryspaces|1 year ago