MCPcopy Create free account
hub / github.com/METR/vivaria / worker

Function worker

pyhooks/pyhooks/python_server.py:171–199  ·  view source on GitHub ↗

Redirects outputs and performs exec

(code: str, output_file, timeout_fyi: float, log: bool)

Source from the content-addressed store, hash-verified

169
170
171def worker(code: str, output_file, timeout_fyi: float, log: bool):
172 "Redirects outputs and performs exec"
173
174 # set up output redirection
175 c = worker_counter_box[0]
176 worker_counter_box[0] += 1
177 stdouts: list = [output_file]
178 stderrs: list = [output_file]
179 if log:
180 stdouts.append(PrefixedFile(real_stdout, f"[python-exec-{c}]- "))
181 stderrs.append(PrefixedFile(real_stderr, f"[python-exec-{c}]+ "))
182 stdout.set_outputs(stdouts)
183 stderr.set_outputs(stderrs)
184
185 # do the exec
186 try:
187 ipython_shell.run_cell(code)
188 except PythonExecTimeoutException:
189 print(
190 f"PythonExecTimeoutException: python exec timed out after {timeout_fyi} seconds",
191 file=stderr,
192 )
193 except PythonExecOutOfMemoryException:
194 print(
195 "PythonExecOutOfMemoryException: python exec exceeded available memory. Python environment has been reset.",
196 file=stderr,
197 )
198 except Exception as e:
199 traceback.print_exception(type(e), e, e.__traceback__, file=stderr)
200
201
202def python_exec_sync(

Callers

nothing calls this directly

Calls 2

PrefixedFileClass · 0.85
set_outputsMethod · 0.80

Tested by

no test coverage detected