summaryrefslogtreecommitdiff
path: root/pysnmp/carrier
diff options
context:
space:
mode:
authorJames Brown <Roguelazer@gmail.com>2017-04-09 02:45:58 -0700
committerIlya Etingof <etingof@gmail.com>2017-04-09 11:45:58 +0200
commit3e6a654a755c22758267c64f2228c34e33ca2a2c (patch)
tree7518b91e7e466d2a2e933188ce0e8d125682a98c /pysnmp/carrier
parent7af816adb13848f7d9e589d61fedde290a553e82 (diff)
downloadpysnmp-git-3e6a654a755c22758267c64f2228c34e33ca2a2c.tar.gz
do not bind to a specific event loop at import time (#53)
Diffstat (limited to 'pysnmp/carrier')
-rw-r--r--pysnmp/carrier/asyncio/dgram/base.py13
-rw-r--r--pysnmp/carrier/asyncio/dgram/udp.py7
-rw-r--r--pysnmp/carrier/asyncio/dgram/udp6.py6
-rw-r--r--pysnmp/carrier/asyncio/dispatch.py11
4 files changed, 12 insertions, 25 deletions
diff --git a/pysnmp/carrier/asyncio/dgram/base.py b/pysnmp/carrier/asyncio/dgram/base.py
index 362d211d..9f234de5 100644
--- a/pysnmp/carrier/asyncio/dgram/base.py
+++ b/pysnmp/carrier/asyncio/dgram/base.py
@@ -41,8 +41,6 @@ try:
except ImportError:
import trollius as asyncio
-loop = asyncio.get_event_loop()
-
class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport):
"""Base Asyncio datagram Transport, to be used with AsyncioDispatcher"""
@@ -50,15 +48,18 @@ class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport):
addressType = lambda x: x
transport = None
- def __init__(self, sock=None, sockMap=None):
+ def __init__(self, sock=None, sockMap=None, loop=None):
self._writeQ = []
self._lport = None
+ if loop is None:
+ loop = asyncio.get_event_loop()
+ self.loop = loop
def datagram_received(self, datagram, transportAddress):
if self._cbFun is None:
raise error.CarrierError('Unable to call cbFun')
else:
- loop.call_soon(self._cbFun, self, transportAddress, datagram)
+ self.loop.call_soon(self._cbFun, self, transportAddress, datagram)
def connection_made(self, transport):
self.transport = transport
@@ -79,7 +80,7 @@ class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport):
def openClientMode(self, iface=None):
try:
- c = loop.create_datagram_endpoint(
+ c = self.loop.create_datagram_endpoint(
lambda: self, local_addr=iface, family=self.sockFamily
)
self._lport = asyncio.async(c)
@@ -89,7 +90,7 @@ class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport):
def openServerMode(self, iface):
try:
- c = loop.create_datagram_endpoint(
+ c = self.loop.create_datagram_endpoint(
lambda: self, local_addr=iface, family=self.sockFamily
)
self._lport = asyncio.async(c)
diff --git a/pysnmp/carrier/asyncio/dgram/udp.py b/pysnmp/carrier/asyncio/dgram/udp.py
index 71c97324..8269992d 100644
--- a/pysnmp/carrier/asyncio/dgram/udp.py
+++ b/pysnmp/carrier/asyncio/dgram/udp.py
@@ -34,13 +34,6 @@ import socket
from pysnmp.carrier.base import AbstractTransportAddress
from pysnmp.carrier.asyncio.dgram.base import DgramAsyncioProtocol
-try:
- import asyncio
-except ImportError:
- import trollius as asyncio
-
-loop = asyncio.get_event_loop()
-
domainName = snmpUDPDomain = (1, 3, 6, 1, 6, 1, 1)
diff --git a/pysnmp/carrier/asyncio/dgram/udp6.py b/pysnmp/carrier/asyncio/dgram/udp6.py
index 7b27820d..29f7ceaf 100644
--- a/pysnmp/carrier/asyncio/dgram/udp6.py
+++ b/pysnmp/carrier/asyncio/dgram/udp6.py
@@ -8,12 +8,6 @@ import socket
from pysnmp.carrier.base import AbstractTransportAddress
from pysnmp.carrier.asyncio.dgram.base import DgramAsyncioProtocol
-try:
- import asyncio
-except ImportError:
- import trollius as asyncio
-
-loop = asyncio.get_event_loop()
domainName = snmpUDP6Domain = (1, 3, 6, 1, 2, 1, 100, 1, 2)
diff --git a/pysnmp/carrier/asyncio/dispatch.py b/pysnmp/carrier/asyncio/dispatch.py
index 20d4d3ab..eea4bd59 100644
--- a/pysnmp/carrier/asyncio/dispatch.py
+++ b/pysnmp/carrier/asyncio/dispatch.py
@@ -40,8 +40,6 @@ try:
except ImportError:
import trollius as asyncio
-loop = asyncio.get_event_loop()
-
class AsyncioDispatcher(AbstractTransportDispatcher):
"""AsyncioDispatcher based on asyncio event loop"""
@@ -52,17 +50,18 @@ class AsyncioDispatcher(AbstractTransportDispatcher):
if 'timeout' in kwargs:
self.setTimerResolution(kwargs['timeout'])
self.loopingcall = None
+ self.loop = kwargs.pop('loop', asyncio.get_event_loop())
@asyncio.coroutine
def handle_timeout(self):
while True:
yield asyncio.From(asyncio.sleep(self.getTimerResolution()))
- self.handleTimerTick(loop.time())
+ self.handleTimerTick(self.loop.time())
def runDispatcher(self, timeout=0.0):
- if not loop.is_running():
+ if not self.loop.is_running():
try:
- loop.run_forever()
+ self.loop.run_forever()
except KeyboardInterrupt:
raise
except Exception:
@@ -95,6 +94,6 @@ if not hasattr(asyncio, "From"):
def handle_timeout(self):
while True:
yield from asyncio.sleep(self.getTimerResolution())
- self.handleTimerTick(loop.time())
+ self.handleTimerTick(self.loop.time())
AsyncioDispatcher.handle_timeout = handle_timeout\
""")