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

Python TypeError:无法实例化含抽象方法stop的MessageListener抽象类

TypeError: 无法实例化包含抽象方法stop的抽象类MessageListener

问题场景

两台配置完全一致的Windows 11设备(相同Python版本、相同依赖包)运行同一Python脚本,其中一台正常执行,另一台抛出如下错误:

Exception has occurred: TypeError
Can't instantiate abstract class MessageListener with abstract method stop
  File "D:\TestStandFramework_2.4\ProjectsServer\eol.com.J1939_download\my_j1939\electronic_control_unit.py", line 44, in __init__
    self._listeners = [MessageListener(self)]
                       ^^^^^^^^^^^^^^^^^^^^^
  File "D:\TestStandFramework_2.4\ProjectsServer\eol.com.J1939_download\TSW_PCAN_J1939_Downloader.py", line 372, in requestPgn_SOFT_ID
    ecu = j1939.ElectronicControlUnit(max_cmdt_packets=16)
  File "D:\TestStandFramework_2.4\ProjectsServer\eol.com.J1939_download\test.py", line 3, in <module>
    TSW_PCAN_J1939_Downloader.requestPgn_SOFT_ID("pcan", "PCAN_USBBUS1", 500000, 0xF5)
TypeError: Can't instantiate abstract class MessageListener with abstract method stop

已尝试重装Python 3.11.9、更换至最新Python版本、切换32/64位版本,问题仍未解决。

相关代码

自定义的MessageListener类:

class MessageListener(Listener):
    """Listens for messages on CAN bus and feeds them to an ECU instance.

    :param j1939.ElectronicControlUnit ecu:
        The ECU to notify on new messages.
    """

    def __init__(self, ecu : ElectronicControlUnit):
        self.ecu = ecu

    def on_message_received(self, msg : can.Message):
        if msg.is_error_frame or msg.is_remote_frame or (msg.is_extended_id == False):
            return

        try:
            self.ecu.notify(msg.arbitration_id, msg.data, msg.timestamp)
        except Exception as e:
            # Exceptions in any callbaks should not affect CAN processing
            logger.error(str(e))

python-can 4.4.0中的Listener抽象类核心部分:

class Listener(metaclass=ABCMeta):
    @abstractmethod
    def on_message_received(self, msg: Message) -> None:
        """处理接收到的消息"""

    @abstractmethod
    def stop(self) -> None:
        """停止处理消息,执行清理操作"""

问题分析

自定义的MessageListener继承自python-can 4.4.0的Listener抽象类,仅实现了on_message_received方法,但未实现Listener中定义的stop抽象方法。根据Python抽象类规则,必须实现所有抽象方法才能实例化类,这就是报错的根本原因。

两台设备表现不同,大概率是其中一台实际安装的python-can版本低于4.4.0——旧版本的Listener可能未将stop标记为抽象方法,因此未实现也能正常运行。

解决方案

给MessageListener添加stop方法的实现,根据需求选择以下两种方案:

方案1:空实现(无需额外清理)

class MessageListener(Listener):
    # 保留原有__init__和on_message_received方法

    def stop(self) -> None:
        # 空实现,满足抽象类要求
        pass

方案2:添加自定义清理逻辑(如果需要)

class MessageListener(Listener):
    # 保留原有__init__和on_message_received方法

    def stop(self) -> None:
        # 这里可以添加资源释放、状态重置等逻辑
        self.ecu = None  # 示例:清空ECU引用

修改后重新运行脚本即可解决实例化失败的问题。

内容的提问来源于stack exchange,提问作者Dávid Incze

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:54:51