Swap Detector: resource.rusage problems
sragsdale at my-deja.com
sragsdale at my-deja.com
Tue Dec 19 14:00:23 EST 2000
I'm writing a wrapper for a very CPU and RAM hungry program. Every so
often this program gets a little too hungry and I need to be warned
about it.
Is there any way to get resource statistics on a program WHILE it's
running? I know that resource.getrusage() can be used to get resource
statistics after a program has finished, but at that point it's too late
for me.
Here's my sample programs that I've been messing around with.
consumer.py just consumes 10 seconds of CPU and increasing amounts of
RAM while reporting its usage, then it exits. foo.py forks a process,
runs 'consumer' in the child and monitors resource usage in the parent.
It doesn't see the child's resource usage until the child exits.
ObSystemSetups:
1-Python 1.5.2 (#8, Jul 13 1999, 17:39:45) [C] on irix646
IRIX64 bach 6.5 07201608 IP28
2-Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66
19990314/Linux (egcs- on linux-i386
Linux fivefifty 2.2.15-3SGI_39smp #1 SMP Mon Jul 24 08:45:16 EDT 2000
i686 unknown
############### consumer.py #################
import time,resource
ltime = time.time()
foo = ['x'] * 10000
bar = foo[:]
ticks = 0
while 1:
if time.time() > ltime + 1:
ticks = ticks + 1
bar.extend(foo)
ltime = time.time()
print "consumer>",resource.getrusage(resource.RUSAGE_SELF)
print
if ticks > 10:
break
#################### foo.py ###################
import resource
import os,sys,time
pid = os.fork()
# original process
if pid:
# monitor our usage and the use of the children
while 1:
# don't use much CPU to do it
time.sleep(1)
val = os.waitpid(pid,os.WNOHANG)
print "1c>",resource.getrusage(resource.RUSAGE_CHILDREN)
print "1s>",resource.getrusage(resource.RUSAGE_SELF)
print
if val[0]:
print "Done. Exiting."
sys.exit(0)
# child process
else:
os.system("python consumer.py")
print "2c>",resource.getrusage(resource.RUSAGE_CHILDREN)
print "2s>",resource.getrusage(resource.RUSAGE_SELF)
os._exit(0)
Sent via Deja.com
http://www.deja.com/
More information about the Python-list
mailing list