gpt4 book ai didi

python - 杀死 Popen 子进程

转载 作者:可可西里 更新时间:2023-11-01 09:54:56 29 4
gpt4 key购买 nike

这是我在社区中发布的第一篇文章,但我已经通过大约 2 周的搜索学习了我所能学到的东西。我似乎陷入了死胡同,我似乎无法通过反复试验或阅读其他帖子找到答案。

此时我要做的就是终止由 Popen 和 shell 本身创建的进程。我必须补充一点,我是编程新手,这是我的第一个“真实”世界方面的任务。

我见过的解决方案说要创建一个组 ID,但那是针对 Unix 的,而我在 Windows 上。 Popen.kill() 和 Popen.terminate() 不会杀死 shell 的子级。这段代码启动的程序不会退出,它的 PID 与 shell 的 PID 不同。我乐于接受建议,因为我将其用作学习工具和富有成效的东西。其他一些解决方案只是不会终止该过程。我可以通过 DOS 和 Python Interpreter 杀死它,但是我必须从 TASKLIST 中手动查找 PID,然后输入它。我需要从这个程序中杀死它。

我正在构建此代码以保存 MathCad 模板的多个文档,该模板读取 excel 文档的值,并吐出具有新文件名的已保存文档。提前致谢。请原谅您可能看到的任何新手编码问题,因为我只是想尝试一下。

其他任务尝试和更新(2/26/2014):

尝试使用 subprocess.call('TASKKILL ...) 而不是 subprocess.Popen 终止程序。它不会杀死 shell 创建的 child 。尝试将 proc = subprocess.Popen(' ....' shell=True ) 替换为 subprocess.call('....', shell=True) 并且 SendKeys 函数不再起作用。尝试使用 kill_proc_tree 函数和“EOFError:[WinError 10054] 现有连接被远程主机强制关闭”弹出窗口的 psutil 函数。子程序仍然没有关闭。

如果我删除 kill/terminate/close 命令并让程序运行,proc.pid 与 os.getpid() 不同。如果我使用 psutil.Process(proc.pid) - 它表示当 Python 解释器将返回其 PID 时该进程不存在。如果我得到 MathCadPrime.exe PID = 例如来自 TASKLIST 的 1111 并使用 psutil.Process(1111).parent,它显示没有父项。这可能是为什么在 shell 上执行 TASKKILL 不会关闭子项的原因?我已经完全按照我写的代码发布了我的代码,看看你们专家是否看到了我没有看到的东西。

#def kill_proc_tree(pid, including_parent=True):
# parent = psutil.Process(pid)
# for child in parent.get_children(recursive=True):
# child.kill()
# if including_parent:
# parent.kill()

import subprocess
import win32api
import win32com.client
import time
import os
import signal
import psutil

# User files
file = "C:\Directory\SubDirectory\file.mcdx"


# Command line function
# /B /D - No command line window or explorer window
proc = subprocess.Popen('start /B /D \
"C:\Program Files\PTC\Mathcad\Mathcad Prime 2.0\" "MathcadPrime.exe" \
"C:\Directory\SubDirectory\file.mcdx"', shell=True)

# Set and test for active window
focus = False
while (focus == False):
focus = shell.AppActivate("MathCad Prime 2.0 - " + file)
time.sleep(1)
if (focus == True):
break

# Send MathCad Prime commands
shell.SendKeys('%', 0)
time.sleep(0.5)
shell.SendKeys('c', 0)
time.sleep(0.1)
shell.SendKeys('c', 0)
time.sleep(2.0)
shell.SendKeys('%', 0)
time.sleep(0.5)
shell.SendKeys('f', 0)
time.sleep(0.1)
shell.SendKeys('s', 0)
time.sleep(4.0)



#Other methods to tried to kill children:

#Method 1:
#subprocess.call("TASKKILL /F /T /PID {pid}".format(pid=proc.pid))

#Method 2:
#subprocess.Popen("TASKKILL /F /T /PID {pid}".format(pid=proc.pid))

#Method 3:
#if (int(proc.pid) > 0):
# os.kill(proc.pid, signal.SIGTERM)

#Method 4 (used with kill_proc_tree):
#proc1 = os.getpid()
#kill_proc_tree(proc1)

最佳答案

感谢大家的帮助,你们帮我得出了结论。与建议的略有不同,但每次都能完成工作。我在家,不在工作(所以我在脑海中写下了这个),但基本上我所做的就是将 TASKLIST 写入文本文件并过滤掉一串代码:

os.chdir('C:\Code')
subprocess.call('TASKLIST /fi "IMAGENAME eq MathcadPrime.exe" /nh /fo csv > task.txt)

然后解析出PID:

doc = open('task.txt', 'rt')
parse = doc.read()
listing = parse.split(",")
PID = listing[1]
PID = int(PID.replace('"','').replace("'",""))
os.kill(PID, signal.SIGTERM)

我希望这可以帮助其他人。

关于python - 杀死 Popen 子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021701/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com