MCPcopy Create free account
hub / github.com/brokermr810/QuantDinger / _worker

Function _worker

backend_api_python/app/utils/safe_exec.py:367–411  ·  view source on GitHub ↗
(code, input_data, max_memory_mb, result_pipe)

Source from the content-addressed store, hash-verified

365 return {'success': False, 'error': f"Unsafe code rejected: {err}", 'result': None}
366
367 def _worker(code, input_data, max_memory_mb, result_pipe):
368 try:
369 if sys.platform != 'win32':
370 try:
371 import resource
372 mem = max_memory_mb * 1024 * 1024
373 resource.setrlimit(resource.RLIMIT_AS, (mem, mem))
374 except Exception:
375 pass
376
377 import numpy as np
378 import pandas as pd
379
380 exec_env = {
381 '__builtins__': build_safe_builtins(),
382 'np': np,
383 'pd': pd,
384 }
385 if input_data:
386 exec_env.update(input_data)
387
388 pre_import = "import numpy as np\nimport pandas as pd\n"
389 exec(pre_import, exec_env)
390 exec(code, exec_env)
391
392 # Extract only picklable, non-module results
393 output = {}
394 for k, v in exec_env.items():
395 if k.startswith('_') or k in ('np', 'pd', '__builtins__'):
396 continue
397 try:
398 pickle.dumps(v)
399 output[k] = v
400 except Exception:
401 pass
402
403 result_pipe.send({'success': True, 'error': None, 'result': output})
404 except Exception as e:
405 result_pipe.send({
406 'success': False,
407 'error': f"{type(e).__name__}: {e}",
408 'result': None,
409 })
410 finally:
411 result_pipe.close()
412
413 parent_conn, child_conn = multiprocessing.Pipe(duplex=False)
414

Callers

nothing calls this directly

Calls 4

build_safe_builtinsFunction · 0.85
updateMethod · 0.80
dumpsMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected