diff options
author | Ilya Etingof <etingof@gmail.com> | 2016-11-05 22:59:31 +0100 |
---|---|---|
committer | Ilya Etingof <etingof@gmail.com> | 2016-11-05 22:59:31 +0100 |
commit | c54a3f6dc8ee433a55001e1cd97f6801bd6e52b7 (patch) | |
tree | 36b8477a686d5e15a8c6ab028626c69e7303eb10 /pysnmp/carrier/asyncio/dispatch.py | |
parent | a0ef4b6ce81683dc33ae00b3dcedd1c4ec282249 (diff) | |
download | pysnmp-git-asyncio-dispatcher-fixes.tar.gz |
WIP: gracefully shutdown asyncio dispatcherasyncio-dispatcher-fixes
Diffstat (limited to 'pysnmp/carrier/asyncio/dispatch.py')
-rw-r--r-- | pysnmp/carrier/asyncio/dispatch.py | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/pysnmp/carrier/asyncio/dispatch.py b/pysnmp/carrier/asyncio/dispatch.py index cb4c0941..4227d100 100644 --- a/pysnmp/carrier/asyncio/dispatch.py +++ b/pysnmp/carrier/asyncio/dispatch.py @@ -51,13 +51,14 @@ class AsyncioDispatcher(AbstractTransportDispatcher): self.__transportCount = 0 if 'timeout' in kwargs: self.setTimerResolution(kwargs['timeout']) - self.loopingcall = None + self._futureTimer = None @asyncio.coroutine - def handle_timeout(self): - while True: - yield asyncio.From(asyncio.sleep(self.getTimerResolution())) - self.handleTimerTick(loop.time()) + def fireTimer(self): + yield asyncio.From(asyncio.sleep(self.getTimerResolution())) + self.handleTimerTick(loop.time()) + if self._futureTimer: + self._futureTimer = asyncio.async(self.fireTimer()) def runDispatcher(self, timeout=0.0): if not loop.is_running(): @@ -69,8 +70,8 @@ class AsyncioDispatcher(AbstractTransportDispatcher): raise PySnmpError(';'.join(traceback.format_exception(*sys.exc_info()))) def registerTransport(self, tDomain, transport): - if self.loopingcall is None and self.getTimerResolution() > 0: - self.loopingcall = asyncio.async(self.handle_timeout()) + if not self._futureTimer and self.getTimerResolution() > 0: + self._futureTimer = asyncio.async(self.fireTimer()) AbstractTransportDispatcher.registerTransport( self, tDomain, transport ) @@ -83,18 +84,20 @@ class AsyncioDispatcher(AbstractTransportDispatcher): self.__transportCount -= 1 # The last transport has been removed, stop the timeout - if self.__transportCount == 0 and not self.loopingcall.done(): - self.loopingcall.cancel() - self.loopingcall = None + if self.__transportCount == 0: + if self._futureTimer: + self._futureTimer.cancel() + self._futureTimer = None # Trollius or Tulip? if not hasattr(asyncio, "From"): - exec ("""\ + exec("""\ @asyncio.coroutine -def handle_timeout(self): - while True: - yield from asyncio.sleep(self.getTimerResolution()) - self.handleTimerTick(loop.time()) -AsyncioDispatcher.handle_timeout = handle_timeout\ +def fireTimer(self): + yield from asyncio.sleep(self.getTimerResolution()) + self.handleTimerTick(loop.time()) + if self._futureTimer: + self._futureTimer = asyncio.async(self.fireTimer()) +AsyncioDispatcher.fireTimer = fireTimer\ """) |