Code: Alles auswählen
f = os.fork()
if f < 0:
print 'error launching!'
usage()
elif f > 0:
# parent
print 'launching child script, PID is %s' % f
print 'sleeping for %d seconds ...' % secs
time.sleep(secs)
print 'Killing processes ...'
os.system("kill `ps ax | grep scrips.php | awk '{print $1}'`")
elif f == 0:
# child
start = time.time()
o, i, e = popen2.popen3('php scrips.php %s' % prog)
err = e.read()
out = o.read()
i.close()
e.close()
o.close()
end = time.time()
print err
analysis = distill(prog, start, end, err)
sys.exit(0)
Hier mein Ansatz:
Code: Alles auswählen
def sub_test(prog, secs):
proc = subprocess.Popen(['php', 'script.php', prog],
shell = False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
print 'PARENT : Pausing before sending signal...'
sys.stdout.flush()
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(secs)
stdout_value = ""
stderr_value = ""
try:
stdout_value, stderr_value = proc.communicate()
signal.alarm(0)
except Alarm:
print "Oops, taking too long!"
finally:
print "out", repr(stdout_value)
print "err", repr(stderr_value)
print 'PARENT : Signaling child'
sys.stdout.flush()
os.kill(proc.pid, signal.SIGKILL)
sys.exit(0)