Python subprocess.Popen返回不带';t存在-windows 10
我正在尝试打开一个进程,等待几分钟,然后关闭它。 我使用的是Windows10和python 3.7。当使用subprocess.Popen时,程序可以很好地打开,但是当我尝试接近使用多个选项时,没有任何效果。 似乎proc.pid不包含有效的pid,所以无论我如何尝试关闭它,都没有任何效果。知道怎么办吗Python subprocess.Popen返回不带';t存在-windows 10,python,operating-system,subprocess,Python,Operating System,Subprocess,我正在尝试打开一个进程,等待几分钟,然后关闭它。 我使用的是Windows10和python 3.7。当使用subprocess.Popen时,程序可以很好地打开,但是当我尝试接近使用多个选项时,没有任何效果。 似乎proc.pid不包含有效的pid,所以无论我如何尝试关闭它,都没有任何效果。知道怎么办吗 import subprocess import signal import os import time import psutil def kill_proc_tree(pid, inc
import subprocess
import signal
import os
import time
import psutil
def kill_proc_tree(pid, including_parent=True):
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
gone, still_alive = psutil.wait_procs(children, timeout=5)
if including_parent:
parent.kill()
parent.wait(5)
def kill_child_processes(parent_pid, sig=signal.SIGTERM):
try:
parent = psutil.Process(parent_pid)
except psutil.NoSuchProcess:
return
children = parent.children(recursive=True)
for process in children:
process.send_signal(sig)
proc = subprocess.Popen([r"C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe",
'--profile-directory=Default',
'--app-id=asdbiflszpsdsdn'])
time.sleep(5)
os.kill(proc.pid, signal.SIGTERM) #or signal.SIGKILL
proc.terminate()
proc.kill()
kill_proc_tree(proc.pid, True)
kill_child_processes(proc.pid)
os.killpg(proc.pid, signal.SIGTERM)
subprocess.Popen("taskkill /F /T /PID %i"%proc.pid , shell=True)
os.kill(proc.pid, signal.SIGTERM)
chrome\u proxy
几乎立即启动chrome
,这可能会启动chrome
本身的另一个实例或与之通信。也就是说,您启动的流程是一个小型工作进程,它完成了自己的工作,并且几乎在您启动它时就结束了。您如何知道subprocess.popen()成功了?你能检查一下吗?你不应该吗?另外,[r”
是打字错误还是python的东西?另外,程序名包含空格,是否需要转义?如何转义windows的东西,因为它们使用转义字符作为路径分隔符?为了确保程序不在奇怪的目录中?