473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Running a subprocess on a "leash"

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 *
Jul 18 '05 #1
1 3142
In article <bl**********@conifold.math.ucdavis.edu>,
Greg Kuperberg <gr**@conifold.math.ucdavis.edu> wrote:
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.