Running a subprocess on a "leash"

Discussion in 'Python' started by Greg Kuperberg, Oct 8, 2003.

  1. I want to run subprocesses with limits on the execution time and the
    output size of the subprocess. If the subprocess exceeds limits, then
    the parent should kill it gracefully (SIGTERM rather than SIGKILL). This
    is a useful safety mechanism if you have a CGI interface that starts
    jobs per user request.

    As a result of this effort, I have a comment and a question:

    (1) I don't think that process control is thought through very well
    in Python. Do I really need os, popen2, fcntl, signal, and select?
    If so, it is not nearly as attractive as, for example, using the cgi
    module to process cgi input.

    (2) I still don't know if I am doing this right. I didn't completely
    understand the recent discussion about the os.kill() function, and in
    general I am not sure if what I have is safe. Is it okay?

    -----------------------------------

    import os,sys,time,popen2,fcntl,signal,select,exceptions

    class ProcError(exceptions.Exception):
    def __init__(me,type):
    if type == 'space': me.args = 'Space limit exceeded!'
    elif type == 'time': me.args = 'Time limit exceeded!'
    else: me.args = 'Unknown error'

    def subprocess(command,timeout=None,spaceout=None):
    proc = popen2.Popen4(command)
    pout = proc.fromchild
    proc.tochild.close()
    output = ''
    try:
    if timeout:
    fcntl.fcntl(pout,fcntl.F_SETFL,os.O_NONBLOCK)
    endtime = time.time() + timeout
    while 1:
    timeleft = endtime - time.time()
    if timeleft <= 0: raise ProcError,'time'
    (ready,toss,toss) = select.select([pout],[],[],timeleft)
    if not ready: raise ProcError,'time'
    if spaceout:
    output += pout.read(spaceout-len(output))
    if len(output) >= spaceout: raise ProcError,'space'
    else:
    output += pout.read()
    if proc.poll() >= 0: break
    else:
    if spaceout: output = pout.read(spaceout)
    else: output = pout.read()
    if proc.poll(): raise ProcError,'space'
    except ProcError:
    print sys.exc_value # Debug
    os.kill(proc.pid,signal.SIGTERM)
    return output
    --
    /\ Greg Kuperberg (UC Davis)
    / \
    \ / Visit the Math ArXiv Front at http://front.math.ucdavis.edu/
    \/ * All the math that's fit to e-print *
     
    Greg Kuperberg, Oct 8, 2003
    #1
    1. Advertising

  2. In article <blvkgi$vs2$>,
    Greg Kuperberg <> wrote:
    .
    .
    .
    >(1) I don't think that process control is thought through very well
    >in Python. Do I really need os, popen2, fcntl, signal, and select?
    >If so, it is not nearly as attractive as, for example, using the cgi

    .
    .
    .
    No, that bundle certainly is *not* convenient for you--but
    the Python run-time library has many different customers.
    I think the suboptimality of popen2(), select(), and so
    on is poorly appreciated. On the other hand, it would be
    *really* hard at this point to get people to agree on a
    different interface.
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
     
    Cameron Laird, Oct 8, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    242
  2. Roman Medina-Heigl Hernandez

    Subprocess module: running an interactive shell

    Roman Medina-Heigl Hernandez, Mar 14, 2009, in forum: Python
    Replies:
    3
    Views:
    1,557
    Karthik Gurusamy
    Mar 15, 2009
  3. David Liang
    Replies:
    3
    Views:
    774
    norseman
    Apr 10, 2009
  4. hiral
    Replies:
    2
    Views:
    605
    Jean-Michel Pichavant
    May 5, 2010
  5. Jason Friedman
    Replies:
    0
    Views:
    228
    Jason Friedman
    Sep 21, 2010
Loading...

Share This Page