asyncio“任务被破坏但它正在等待!”在pysnmp示例程序中

时间:2016-09-01 07:53:57

标签: python python-3.x python-asyncio pysnmp

我正在测试pysnmp asyncio模块,并且开始使用随文档提供的sample program。当我运行示例程序时,它会出现Task was destroyed but it is pending!错误。我检查了SO的类似问题,找不到我(没经验的)眼睛有什么问题。我在Debian 8.5上使用Python 3.4.2和随附的asyncio以及pysnmp(4.3.2)

我使用的程序(与pysnmp文档中的示例程序相同)

 
    import asyncio
    from pysnmp.hlapi.asyncio import *


    @asyncio.coroutine
    def getone(snmpEngine, hostname):
        errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd(
            snmpEngine,
            CommunityData('public'),
            UdpTransportTarget(hostname),
            ContextData(),
            ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
        )

        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'
            )
                  )
        else:
            for varBind in varBinds:
                print(' = '.join([x.prettyPrint() for x in varBind]))


    @asyncio.coroutine
    def getall(snmpEngine, hostnames):
        for hostname in hostnames:
            yield from getone(snmpEngine, hostname)


    snmpEngine = SnmpEngine()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161),
                                                ('demo.snmplabs.com', 2161),
                                                ('demo.snmplabs.com', 3161)]))
  

错误是:

 

     Executing  wait_for= cb=[_raise_stop_error() at /usr/lib/python3.4/asyncio/base_event
    s.py:101] created at /usr/lib/python3.4/asyncio/base_events.py:264> took 0.460 seconds
    SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
    SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
    SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
    Task was destroyed but it is pending!
    source_traceback: Object created at (most recent call last):
      File "multiple-sequential-queries.py", line 58, in 
        ('demo.snmplabs.com', 3161)]))   
      File "/usr/lib/python3.4/asyncio/base_events.py", line 271, in run_until_complete
        self.run_forever()
      File "/usr/lib/python3.4/asyncio/base_events.py", line 244, in run_forever
        self._run_once()
      File "/usr/lib/python3.4/asyncio/base_events.py", line 1075, in _run_once
        handle._run()
      File "/usr/lib/python3.4/asyncio/events.py", line 120, in _run
        self._callback(*self._args)
      File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
        result = next(coro)
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__
        return next(self.gen)
      File "multiple-sequential-queries.py", line 50, in getall
        yield from getone(snmpEngine, hostname)
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__
        return next(self.gen)
      File "multiple-sequential-queries.py", line 31, in getone
        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__
        return next(self.gen)
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 141, in coro
        res = func(*args, **kw)
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/hlapi/asyncio/cmdgen.py", line 138, in getCmd
        addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget)
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/hlapi/lcd.py", line 87, in configure
        transport
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/entity/config.py", line 308, in addTransport
        transport)
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/carrier/asyncio/dispatch.py", line 70, in registerTransport
        self.loopingcall = asyncio.async(self.handle_timeout())
    task: :4> wait_for= created at /usr/local/lib/python3.4/dist-packages/pysnmp/carrier/asyncio/dispatch.py:70>

 

非常感谢任何帮助解决这个问题!

感谢。

1 个答案:

答案 0 :(得分:0)

pysnmp中有一个内部定时器功能,用于处理缓存,重试等。使用asyncio传输时,定时器由asyncio Future驱动。您观察到的消息警告您,在关闭主循环之前,Future对象仍然处于挂起状态。

要解决此问题,您需要在完成所有SNMP I / O后取消挂起的计时器任务。一种[黑客]方法是将以下代码段附加到您提到的示例脚本中:

...

snmpEngine = SnmpEngine()

loop = asyncio.get_event_loop()

loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161),
                                            ('demo.snmplabs.com', 2161),
                                            ('demo.snmplabs.com', 3161)]))

# run a coroutine to cancel pending timer task

from pysnmp.hlapi.asyncore.cmdgen import lcd

@asyncio.coroutine
def unconfigure(snmpEngine, authData=None):
    lcd.unconfigure(snmpEngine, authData)

loop.run_until_complete(unconfigure(snmpEngine))

我正在努力将类似的协同程序添加到pysnmp中,以便您可以开箱即用。