Ich habe ein Python-Skript 'b.py', das die Zeit alle 5 Sekunden ausgibt.Wie kann ich ein Python-Skript aus einem Python-Skript aufrufen
while (1):
print "Start : %s" % time.ctime()
time.sleep(5)
print "End : %s" % time.ctime()
time.sleep(5)
Und in meinem a.py, nenne ich b.py von:
def run_b():
print "Calling run b"
try:
cmd = ["./b.py"]
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
print (">>>" + line.rstrip())
except OSError as e:
print >>sys.stderr, "fcs Execution failed:", e
return None
und später, ich töten 'b.py' von: PS_PATH = „/ usr/bin/ps -efW "
def kill_b(program):
try:
cmd = shlex.split(PS_PATH)
retval = subprocess.check_output(cmd).rstrip()
for line in retval.splitlines():
if program in line:
print "line =" + line
pid = line.split(None)[1]
os.kill(int(pid), signal.SIGKILL)
except OSError as e:
print >>sys.stderr, "kill_all Execution failed:", e
except subprocess.CalledProcessError as e:
print >>sys.stderr, "kill_all Execution failed:", e
run_b()
time.sleep(600)
kill_b("b.py")
Ich habe 2 Fragen. 1. Warum sehe ich keine Ausdrucke aus "b.py" und wenn ich "ps -efW" sehe, sehe ich keinen Prozess namens "b.py"? 2. Warum, wenn ich einen Prozess wie oben töte, sehe ich 'Erlaubnis abgelehnt'?
Ich laufe über Skript auf Cygwin unter Windows.
Vielen Dank.
Inception python – Nick
ein einfacherer Weg zu töten 'b.py' ist,' p' aus 'run_b()' zurückzugeben, bevor man 'b.py's Ausgabe liest und' p.kill(); p.wait() 'später. – jfs
Überprüfen Sie, ob './B.py' erfolgreich gestartet wurde (' p.poll() 'sollte' None' sein) – jfs