用 Subprocess.Popen 只能获得其中一个进程的 PID
|
ipwxMay 24, 2020
1
proc = subprocess.Popen(...)
proc.pid 不行嘛? |
|
ipwxMay 24, 2020
3
|
|
AX5NMay 24, 2020
4
什么叫做 2 个进程,是 2 个无关的进程?那你不能写成两句话么。还是进程会启动子进程?
|
|
5
你看我 start 了第一个程序,但是这个程序引起了另外一个进程,我只能获得这个被引起的进程的 pid,也就是第二个进程的 pid,获得不了主进程的 pid,我用 psutil.ppid 获得父进程的 pid 也是不对的 |
|
ipwxMay 24, 2020
10
@reaCodes 不熟悉 windows 。这只能调用 windows api 了,超出了普通程序模型的范畴。或者某个库帮你封装好一个?比如 psutil 这个也许可用:
https://psutil.readthedocs.io/en/latest/#psutil.Process.children |
|
AX5NMay 24, 2020
12
@reaCodes
``` process = psutil.Process(parent_pid) child = process.children(recursive=True) for i in child: print(i) ``` |
|
AX5NMay 24, 2020
14
@reaCodes `subprocess.Popen("cmd.exe", creationflags = subprocess.CREATE_NEW_CONSOLE)`
|
|
ClericPyMay 24, 2020
16
用 psutil 迭代出来挨个排除吧... 我清理 Chrome 就是这么干的...
|
|
JCZ2MkKb5S8ZX9pqMay 24, 2020 via iPhone
17
各个位置分别打一下 os.getpid() 位置要找对
|