Confusion with resource.setrlimit() and subprocesses

Mitja Trampus nun at example.com
Wed Jul 5 07:20:18 EDT 2006


I am trying to run a subprocess within given time and memory restrictions.
The resource module kind of works for me, but I do not understand why and am seeking an 
explanation. Also, the signal module is not behaving as I'd expect it to.

Demo code with questions:

==========
import subprocess as sub
import resource as r
import signal as sig
import time, sys

def exceed(signal, stackFrame):
   "log what we have caught, then exit"
   f=open('/tmp/sig')
   f.write('caught %d, elapsed %d\n' % (signal, time.time()-t)
   f.close()
   sys.exit(0)

print 'running'

t = time.time()

r.setrlimit(r.RLIMIT_CPU,(1,1))
r.setrlimit(r.RLIMIT_AS,(100000,100000))
sig.signal(sig.SIGXCPU, exceed)
sig.signal(sig.SIGSEGV, exceed)

try:
   retcode=sub.call('./hog')
   print 'done with', retcode   ### QUESTION 1
except MemoryError:
   print 'exceeds mem' ### QUESTION 2

# a time consuming loop
for i in xrange(1000000000):
   x = 5.23/123.33  ### QUESTION 3
===========

QUESTION 1:
When ./hog exceeds time limit, retcode==-9, meaning ./hog got a SIGKILL. This is fine with 
me, but why doesn't the python script itself get the SIGKILL? Is it because it is idly 
waiting for the child process to finish, thus using hardly any CPU? If both processes are 
indeed so loosely coupled, I wouldn't expect the child to inherit the limits - which part 
of the docs did I miss?

QUESTION 2:
Why does the subprocess module throw this exception? I thought my sig.signal(sig.SIGSEGV, 
exceed) would kick in.

QUESTION 3:
Similarly, if I do the time-expensive stuff inside the python script, why do I receive 
SIGKILL immediately, instead of one second after SIGXCPU (which the script should catch)

TIA,
Mitja



More information about the Python-list mailing list