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

如何在Python-GStreamer子类中调用父类do_方法避免无限递归?

Python-GStreamer Gst.Bin子类消息处理的无限递归解决办法

在PyGObject实现Gst.Bin子类时,直接调用do_*前缀的父类方法会触发子类的覆盖逻辑,导致无限递归——这是因为do_*是虚方法的实现入口,PyGObject会自动将父类的虚方法调用路由到子类的实现上。

要只自定义ERROR类型消息的处理,同时保留父类对其他消息的逻辑,你可以用两种方式避免递归:

方法一:调用父类的公共方法(推荐)

直接调用不带do_前缀的父类公共方法,它会执行父类自身的虚方法实现,不会触发子类的do_handle_message:

class MyBin(Gst.Bin):
    """New Gst.Bin based Element"""

    def __init__(self):
        super().__init__()
        # 你的初始化逻辑

    def do_handle_message(self, message):
        if message.type != Gst.MessageType.ERROR:
            # 调用父类的原始消息处理逻辑,无递归
            super().handle_message(message)
        else:
            # 自定义ERROR消息的处理逻辑
            err, debug = message.parse_error()
            print(f"Custom error handling: {err.message} (debug: {debug})")
            # 若需要,也可在此处选择性调用父类逻辑,或完全替换

方法二:直接调用父类的虚方法实现

如果你需要直接调用父类的do_handle_message实现,可以显式指定父类作为第一个参数传入,绕过子类的覆盖:

class MyBin(Gst.Bin):
    """New Gst.Bin based Element"""

    def __init__(self):
        super().__init__()
        # 你的初始化逻辑

    def do_handle_message(self, message):
        if message.type != Gst.MessageType.ERROR:
            # 直接调用父类的do_handle_message实现
            Gst.Bin.do_handle_message(Gst.Bin, self, message)
        else:
            # 自定义ERROR消息的处理逻辑
            err, debug = message.parse_error()
            print(f"Custom error handling: {err.message} (debug: {debug})")

两种方法都能避免无限递归,其中方法一更符合Python的面向对象规范,推荐使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 05:42:21