1

I 'd like to create a process in Python (probably with subprocess and Popen), which should have limited CPU time, limited child processess and memory bandwidth. I can;t find a way to do this. resource.setrlimit does not seem to work.

My code is :

import os
import sys
import resource
import subprocess
import signal


def setlimits():
    os.seteuid(65534) # Has to run as root user in order to be able to setuid
    resource.setrlimit(resource.RLIMIT_CPU, (1, 1))
    resource.setrlimit(resource.RLIMIT_FSIZE, (500, 500))
    resource.setrlimit(resource.RLIMIT_NPROC, (80, 80))

p = subprocess.Popen( ["./exec.out"] , preexec_fn=setlimits )
Paris
  • 6,323
  • 7
  • 31
  • 49
  • 2
    Would [this](http://stackoverflow.com/questions/1689505/python-ulimit-and-nice-for-subprocess-call-subprocess-popen) work for you? – MrGomez Mar 28 '12 at 22:21
  • 1
    In what way does it not work? Can you provide a ten-line reproducer program? – sarnold Mar 28 '12 at 22:22
  • http://www.getcode.me/brkeo Here is my code. It does not even stop an eternal loop – Paris Mar 28 '12 at 22:31
  • @Paris you should copy and format your code directly in your question. – Zenon Mar 28 '12 at 22:50
  • You could try this to verify the limits are getting set as you expect for the subprocess `p = subprocess.Popen( ["/bin/bash","-c","ulimit -a"] , preexec_fn=setlimits )` – Brian Swift Mar 28 '12 at 23:21
  • Seems to take effect for me on osx – jdi Mar 28 '12 at 23:28
  • Well, I run it on OS X too. My exec.out is an executable from an infinite loop C file : #include int main () { while ( 1 ) printf( "Hello\n" ); } And the program won't stop unless I hit Ctrl + C – Paris Mar 29 '12 at 08:10
  • All of the system overhead associated with the `printf("Hello\n)` could cause the program to take a long time to accumulate one second of CPU time. Try an infinite loop doing calculations instead of I/O. – Brian Swift Mar 29 '12 at 19:53

0 Answers0