470,811 Members | 1,101 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

Confusion with resource.setrlimit() and subprocesses

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
Jul 5 '06 #1
0 1113

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jane Austine | last post: by
2 posts views Thread by Marcos | last post: by
2 posts views Thread by Dave Kirby | last post: by
9 posts views Thread by Patrick Fisher | last post: by
4 posts views Thread by FDude | last post: by
3 posts views Thread by Dara Durum | last post: by
1 post views Thread by Gal Diskin | last post: by
4 posts views Thread by GiJeet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.