Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 399e556

Browse files
author
Philip Guo
committed
bam
1 parent aaaf22b commit 399e556

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

TODO

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,30 @@ print x
184184
which "list" is which???
185185
'''
186186

187+
---
188+
2011-10-12
189+
190+
Code snippets from Adam Hartz for time-limiting a Python child process:
191+
192+
def setlimits():
193+
"""
194+
Helper to set CPU time limit for check_code, so that infinite loops
195+
in submitted code get caught instead of running forever.
196+
"""
197+
resource.setrlimit(resource.RLIMIT_CPU, (10, 10))
198+
199+
def check_python_code(code):
200+
#TODO: Investigate pypy for sandboxing
201+
"""
202+
Run the given code; outputs a tuple containing:
203+
the program's output (stdout), error messages (stderr)
204+
"""
205+
python = subprocess.Popen(["python"],stdin=subprocess.PIPE,\
206+
stdout=subprocess.PIPE,\
207+
stderr=subprocess.PIPE,\
208+
preexec_fn=setlimits)
209+
PY_PROCESS_LOCK.acquire()
210+
output = python.communicate(code)
211+
PY_PROCESS_LOCK.release()
212+
return output
213+

0 commit comments

Comments
 (0)