Skip to content

Commit

Permalink
asyncio.async deprecated since 3.4.4 (#143)
Browse files Browse the repository at this point in the history
* asyncio.async deprecated since 3.4.4
  • Loading branch information
wallies authored and etingof committed Apr 21, 2018
1 parent 80b09eb commit 2b27b49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pysnmp/carrier/asyncio/dgram/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# THE POSSIBILITY OF SUCH DAMAGE.
#
import sys
import platform
import traceback
from pysnmp.carrier.asyncio.base import AbstractAsyncioTransport
from pysnmp.carrier import error
Expand Down Expand Up @@ -77,13 +78,20 @@ def connection_lost(self, exc):
debug.logger & debug.flagIO and debug.logger('connection_lost: invoked')

# AbstractAsyncioTransport API


python344 = platform.python_version_tuple() >= ('3', '4', '4')

def openClientMode(self, iface=None):
try:
c = self.loop.create_datagram_endpoint(
lambda: self, local_addr=iface, family=self.sockFamily
)
self._lport = asyncio.async(c)
# Avoid deprecation warning for asyncio.async()
if python344:
self._lport = asyncio.ensure_future(c)
else: # pragma: no cover
self._lport = asyncio.async(c)

except Exception:
raise error.CarrierError(';'.join(traceback.format_exception(*sys.exc_info())))
return self
Expand Down
10 changes: 8 additions & 2 deletions pysnmp/carrier/asyncio/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# THE POSSIBILITY OF SUCH DAMAGE.
#
import sys
import platform
import traceback
from pysnmp.carrier.base import AbstractTransportDispatcher
from pysnmp.error import PySnmpError
Expand All @@ -40,6 +41,7 @@
except ImportError:
import trollius as asyncio

python344 = platform.python_version_tuple() >= ('3', '4', '4')

class AsyncioDispatcher(AbstractTransportDispatcher):
"""AsyncioDispatcher based on asyncio event loop"""
Expand All @@ -66,10 +68,14 @@ def runDispatcher(self, timeout=0.0):
raise
except Exception:
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())
# Avoid deprecation warning for asyncio.async()
if python344:
self.loopingcall = asyncio.ensure_future(self.handle_timeout())
else: # pragma: no cover
self.loopingcall = asyncio.async(self.handle_timeout())
AbstractTransportDispatcher.registerTransport(
self, tDomain, transport
)
Expand Down

0 comments on commit 2b27b49

Please sign in to comment.