Skip to content

Commit

Permalink
util/snmp: use ensure_event_loop
Browse files Browse the repository at this point in the history
Use the new labgrid.loop package with ensure_event_loop to ensure we
have an event loop for pysnmp.

Signed-off-by: Rouven Czerwinski <[email protected]>
  • Loading branch information
Emantor committed Sep 26, 2024
1 parent ae3f3b3 commit aa4421a
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions labgrid/util/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pysnmp.hlapi.v3arch.asyncio as hlapi
from ..driver.exception import ExecutionError
from ..loop import ensure_event_loop


class SimpleSNMP:
Expand All @@ -10,15 +11,7 @@ def __init__(self, host, community, port=161):
if port is None:
port = 161

self.loop_created = False

try:
# if called from async code, try to get current's thread loop
self.loop = asyncio.get_running_loop()
except RuntimeError:
self.loop_created = True
# no previous, external or running loop found, create a new one
self.loop = asyncio.new_event_loop()
self.loop = ensure_event_loop()

self.engine = hlapi.SnmpEngine()
self.transport = self.loop.run_until_complete(hlapi.UdpTransportTarget.create((host, port)))
Expand Down Expand Up @@ -47,6 +40,5 @@ def set(self, oid, value):

def cleanup(self):
self.engine.closeDispatcher()
if self.loop_created:
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
self.loop.close()
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
self.loop.close()

0 comments on commit aa4421a

Please sign in to comment.