Confusion with resource.setrlimit() and subprocesses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mitja Trampus

    #1

    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.R LIMIT_CPU,(1,1) )
    r.setrlimit(r.R LIMIT_AS,(10000 0,100000))
    sig.signal(sig. SIGXCPU, exceed)
    sig.signal(sig. SIGSEGV, exceed)

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

    # a time consuming loop
    for i in xrange(10000000 00):
    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
Working...