Wie, um die gewünschten Ergebnisse zu erzielen, wenn mit der subprocees Popen.send_signal(CTRL_C_EVENT) in Windows?
In python 2.7 in windows entsprechend der Dokumentation, die Sie senden können, ein CTRL_C_EVENT
(Python 2.7 Subprocess Popen.send_signal Dokumentation).
Allerdings, wenn ich versuchte es ich nicht erhalten das erwartete Tastatur-interrupt in den Teilprozess.
Dies ist der Beispiel-code für den parent-Prozess:
# FILE : parentProcess.py
import subprocess
import time
import signal
CREATE_NEW_PROCESS_GROUP = 512
process = subprocess.Popen(['python', '-u', 'childProcess.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
creationflags=CREATE_NEW_PROCESS_GROUP)
print "pid = ", process.pid
index = 0
maxLoops = 15
while index < maxLoops:
index += 1
# Send one message every 0.5 seconds
time.sleep(0.5)
# Send data to the subprocess
process.stdin.write('Bar\n')
# Read data from the subprocess
temp = process.stdout.readline()
print temp,
if (index == 10):
# Send Keyboard Interrupt
process.send_signal(signal.CTRL_C_EVENT)
Dies ist der Beispielcode für das Kind proceess:
# FILE : childProcess.py
import sys
while True:
try:
# Get data from main process
temp = sys.stdin.readline()
# Write data out
print 'Foo ' + temp,
except KeyboardInterrupt:
print "KeyboardInterrupt"
Wenn ich die Datei ausführen parentProcess.py ich erwarte, dass man "Foo Bar" zehn mal dann ein "KeyboardInterrupt", gefolgt von "Foo Bar" 4 mal aber ich bekomme "Foo Bar" 15 mal statt.
Ist es ein Weg, um die CTRL_C_EVENT verhält sich wie eine Tastatur-interrupt nur als SIGINT verhält sich in Linux?
Nach einigen Lesen fand ich einige Informationen, die scheint contradic die python-Dokumentation über CTRL_C_EVENT, insbesondere, sagt, dass
CTRL_C_EVENT
0 Erzeugt eine STRG+C-signal. Dieses signal nicht generiert werden können, für die Prozess-Gruppen
Folgende Website bieten weitere Informationen über die Schöpfung flags:
Prozess-Erstellung Flags.
InformationsquelleAutor Mario | 2011-02-17
Du musst angemeldet sein, um einen Kommentar abzugeben.
Diese Methode der signal-handling durch Teilprozesse arbeitete für mich auf Linux-und Windows 2008 sowohl mit Python 2.7.2, aber es nutzt Ctrl-Break anstelle von Strg-C. Siehe Hinweis zu Prozess-Gruppen und Strg-C in http://msdn.microsoft.com/en-us/library/ms683155%28v=vs.85%29.aspx.
catcher.py:
thrower.py:
InformationsquelleAutor Craig McDaniel
versuchen mit
oder
Referenzen:
http://docs.activestate.com/activepython/2.5/pywin3/win32process_CREATE_NEW_PROCESS_GROUP.html
http://msdn.microsoft.com/en-us/library/ms683155%28v=vs.85%29.aspx
Lesen info über dwProcessGroupId, die groupid, sollte das gleiche für die Prozess-id
InformationsquelleAutor sherpya