File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -184,3 +184,30 @@ print x
184
184
which "list" is which???
185
185
'''
186
186
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
+
You can’t perform that action at this time.
0 commit comments