Python 3.14下 PySNMP陷阱接收端的运行时错误

编程语言 2026-07-09

我正在使用PySNMP 7.1.26与 Python 3.14。

我已经构建了一个trap接收器,将其放在另一个进程中启动,以便与我的主代码并行运行(我没有发现其他选项)。

部分代码(取自PySNMP示例):

from pysnmp.carrier.asyncio.dispatch import AsyncioDispatcher
from pysnmp.carrier.asyncio.dgram import udp        

"""skip some code (call back definition)"""

transportDispatcher = AsyncioDispatcher()
transportDispatcher.register_recv_callback(__callback)
# UDP/IPv4
transportDispatcher.register_transport(
    udp.DOMAIN_NAME, udp.UdpAsyncioTransport().open_server_mode((host, port))
)
transportDispatcher.job_started(1)
try:
    transportDispatcher.run_dispatcher(timeout=time_out)
finally:
    transportDispatcher.close_dispatcher()

这段代码在Python 3.11 -> 3.13运行正常。切换到3.14时,我遇到了运行时错误:

  File "xxx\Python\Python314\Lib\multiprocessing\process.py", line 320, in _bootstrap                                                                                                          
    self.run()         

  File "xxx\Python\Python314\Lib\multiprocessing\process.py", line 108, in run                                                                                                                 
    self._target(*self._args, **self._kwargs)  

  File "xxxy\venv314\Lib\site-packages\protocols\Snmp\SnmpTrapReceiver.py", line 93, in trap_listener_v1                                                                 
    transportDispatcher = AsyncioDispatcher()     

  File "xxxx\Python\Python314\Lib\asyncio\events.py", line 715, in get_event_loop                                                                                                               
    raise RuntimeError('There is no current event loop in thread %r.'                                                                                                                                                                 
                       % threading.current_thread().name)                                                                                                                                                                             
RuntimeError: There is no current event loop in thread 'MainThread'.

同样的错误也出现在v3 trap receiver上:

  File "xxx\venv_314\Lib\site-packages\pysnmp\carrier\asyncio\dgram\base.py", line 100, in __init__
    loop = asyncio.get_event_loop()
  File "xxx\Python\Python314\Lib\asyncio\events.py", line 715, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
                       % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'MainThread'.

这是一个问题吗,还是我遗漏了什么?

我在3.14中发现了这个变动:

asyncio.get_event_loop() 现在在没有当前事件循环时会抛出一个 RuntimeError,并且不再隐式创建事件循环。

这是否是问题的根本原因?

解决方案

也许这更适合作为普通注释。

我认为这可能是导致该错误的原因。

因此你可能需要等待更新的 pysnmp,它将能够与 3.14 一起工作。

你也可以在GitHub上把这个问题提交给pysnmp的作者,作为一个 issue 来处理。
但请先检查是否已有其他人报告过这个问题。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章