You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Davinci Resolve子进程检测代码问题排查与优化咨询

DaVinci Resolve进程检测问题排查与优化

一、DaVinci Resolve启动进程流程

  • 启动DaVinci Resolve时,加载阶段会启动名为Resolve的子进程
    Resolve子进程
  • 加载完成后,Resolve子进程关闭,同时启动名为Project Manager的子进程
    Project Manager子进程

二、现有代码的问题

你提供的代码存在以下明确错误:

  • 函数未定义:遍历进程的逻辑没有封装成is_process_running函数,后续调用时会触发NameError
  • 变量未定义:遍历逻辑中用到的process_name没有传入或定义
  • 引号格式错误:代码中使用了中文引号(“”),Python仅支持英文引号(""),会引发语法错误
  • 冗余导入:导入了subprocess但未实际使用,属于无效代码

修正后的基础可用代码:

import time
import psutil

def is_process_running(process_name):
    for proc in psutil.process_iter(['pid', 'name']):
        try:
            if proc.info['name'] == process_name:
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied):
            # 忽略已结束或无权限访问的进程
            continue
    return False

print("Waiting for the 'Project Manager' sub-process")
while not is_process_running("Project Manager.exe"):
    time.sleep(1)

print("Subprocess 'Project Manager' is open")

三、Windows内置命令替代方案

不需要依赖psutil,可以通过Windows自带的tasklist命令结合subprocess实现进程检测:

import subprocess
import time

def is_process_running(process_name):
    result = subprocess.run(
        ['tasklist', '/fi', f'IMAGENAME eq {process_name}'],
        capture_output=True,
        text=True
    )
    # 排除空结果的情况,确认进程存在
    return process_name in result.stdout and 'INFO: No tasks are running' not in result.stdout

print("Waiting for the 'Project Manager' sub-process")
while not is_process_running("Project Manager.exe"):
    time.sleep(1)

print("Subprocess 'Project Manager' is open")

四、更高效可靠的检测方式

time.sleep(1)轮询属于被动等待,效率较低,推荐两种更优方案:

1. 实时监听进程创建事件(Windows API)

通过pywin32调用Windows WMI接口,实时监听新进程创建事件,无需轮询:

import win32com.client
import pythoncom

def wait_for_process(process_name):
    pythoncom.CoInitialize()
    wmi = win32com.client.GetObject("winmgmts:")
    # 设置0.1秒的监听间隔,接近实时触发
    watcher = wmi.ExecNotificationQuery(
        "SELECT * FROM __InstanceCreationEvent WITHIN 0.1 WHERE TargetInstance ISA 'Win32_Process'"
    )
    while True:
        event = watcher.NextEvent()
        if event.TargetInstance.Name == process_name:
            print(f"Subprocess '{process_name}' is open")
            break

wait_for_process("Project Manager.exe")

这种方式无轮询延迟,是效率最高的检测方式。

2. 关联父进程检测

由于Project Manager是DaVinci Resolve的子进程,可以先定位主Resolve进程,再监听其子进程创建,避免误判其他同名进程:

import psutil
import time

def get_main_resolve_pid():
    for proc in psutil.process_iter(['pid', 'name']):
        if proc.info['name'] == "Resolve.exe":
            return proc.info['pid']
    return None

def wait_for_child_process(parent_pid, child_name):
    while True:
        try:
            parent = psutil.Process(parent_pid)
            for child in parent.children(recursive=True):
                if child.name() == child_name:
                    print(f"Subprocess '{child_name}' is open")
                    return
        except psutil.NoSuchProcess:
            print("Main Resolve process exited before Project Manager started")
            return
        time.sleep(0.5)

main_pid = get_main_resolve_pid()
if main_pid:
    wait_for_child_process(main_pid, "Project Manager.exe")
else:
    print("Main Resolve process not found")

这种方式准确性更高,不会混淆系统中的其他同名进程。

内容的提问来源于stack exchange,提问作者Phill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 22:59:53