等待子进程启动

问题描述 投票:0回答:1

当我启动 Davinci Resolve 时,它在加载时会启动一个名为 Resolve 的子进程。enter image description here

然后,当它完成加载时,该子进程将关闭并打开另一个称为项目管理器的子进程。 enter image description here

import subprocess
import time
import psutil


for proc in psutil.process_iter(['pid', 'name']):
    if proc.info['name'] == process_name:
        return True
return False

print(“Waiting for the 'Project Manager to open' sub-process”)
while not is_process_running("Project Manager.exe"):
    time.sleep(1)  # Aguarda 1 segundo antes de verificar novamente

print("Subprocess 'Project Manager' open ")

但是,代码有问题,我不知道它是什么。

我尝试过使用 psutil,但我不知道是否真的有必要执行我想要的操作,或者是否有内部 Windows 命令可以正确执行此操作。

另外,是否有比使用 time.sleep(1) 更快、更可靠的方法来检查项目经理子流程是否已打开并尝试再次检查?

python windows subprocess
1个回答
0
投票

我尝试使用 Michael 指示的 Children 方法,但代码甚至没有找到主要的 DaVinci Resolve 流程

import psutil
import time

# get main subprocess
for proc in psutil.process_iter(['pid', 'name']):
    if proc.info['name'] == 'DaVinci Resolve':
        resolve_proc = psutil.Process(proc.info['pid'])
        break
else:
    raise Exception("DaVinci Resolve not found")


# Checks that the Project Manager subprocess is open
project_manager_found = False

while not project_manager_found:

    children = resolve_proc.children(recursive=True)
    for child in children:
        if child.name() == 'Project Manager':
            project_manager_found = True
            print("Subprocess 'Project Manager' not found.")
            break
    
    if not project_manager_found:
        print("Subprocess 'Project Manager'not found. checking again in 1 second")
        time.sleep(1)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.