我读了很多stackoverflow上的问题,但它们都太旧了,对我没有帮助。 我有一个子进程,并想发送CTRL_C_EVENT信号来停止它。我不想直接杀掉它。 以下是我的代码:
import subprocess
import os
import signal
CREATE_NO_WINDOW = 0x08000000
'''
I tried these too but i failed.
creationflags=CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS
CREATE_NEW_PROCESS_GROUP = 0x00000200
DETACHED_PROCESS = 0x00000008
'''
cmd = 'my cmd arguments'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True,shell=True,creationflags=CREATE_NO_WINDOW)
test = 0
for line in process.stdout:
test += 1
if (test > 60):
os.kill(process.pid, signal.CTRL_C_EVENT)
#This fails too
#process.send_signal(signal.CTRL_C_EVENT)
else:
print(line)
这里是异常:
OSError: [WinError 6] The handler is invalid
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\xxxxxxx\Desktop\xxxxx\test subprocess.py", line 16, in <module>
os.kill(process.pid, signal.CTRL_C_EVENT)
SystemError: <built-in function kill> returned a result with an error set