473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,pop en2,fcntl,signa l,select,except ions

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

def subprocess(comm and,timeout=Non e,spaceout=None ):
proc = popen2.Popen4(c ommand)
pout = proc.fromchild
proc.tochild.cl ose()
output = ''
try:
if timeout:
fcntl.fcntl(pou t,fcntl.F_SETFL ,os.O_NONBLOCK)
endtime = time.time() + timeout
while 1:
timeleft = endtime - time.time()
if timeleft <= 0: raise ProcError,'time '
(ready,toss,tos s) = select.select([pout],[],[],timeleft)
if not ready: raise ProcError,'time '
if spaceout:
output += pout.read(space out-len(output))
if len(output) >= spaceout: raise ProcError,'spac e'
else:
output += pout.read()
if proc.poll() >= 0: break
else:
if spaceout: output = pout.read(space out)
else: output = pout.read()
if proc.poll(): raise ProcError,'spac e'
except ProcError:
print sys.exc_value # Debug
os.kill(proc.pi d,signal.SIGTER M)
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 3165
In article <bl**********@c onifold.math.uc davis.edu>,
Greg Kuperberg <gr**@conifold. math.ucdavis.ed u> wrote:
Jul 18 '05 #2

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

Similar topics

169
9219
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 untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to embark on creating a nice little Small C++ compiler devoid of C++ language features that make...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.