From 0dca0ac18816a5b9b70d29ab8cb2f2d87b818822 Mon Sep 17 00:00:00 2001 From: mastercoms Date: Fri, 18 Oct 2019 15:46:42 -0400 Subject: [PATCH 1/3] add coroutine support for attribute listeners --- dronekit/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index 4f3613906..58adbe8e6 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -32,6 +32,7 @@ ---- """ +import asyncio import collections import copy import logging @@ -642,13 +643,19 @@ def notify_attribute_listeners(self, attr_name, value, cache=False): # Notify observers. for fn in self._attribute_listeners.get(attr_name, []): try: - fn(self, attr_name, value) + if asyncio.iscoroutinefunction(fn): + asyncio.run(fn(self, attr_name, value)) + else: + fn(self, attr_name, value) except Exception: self._logger.exception('Exception in attribute handler for %s' % attr_name, exc_info=True) for fn in self._attribute_listeners.get('*', []): try: - fn(self, attr_name, value) + if asyncio.iscoroutinefunction(fn): + asyncio.run(fn(self, attr_name, value)) + else: + fn(self, attr_name, value) except Exception: self._logger.exception('Exception in attribute handler for %s' % attr_name, exc_info=True) From 8e3b47bce4789bb3be18cc7517decf8a6fb2e5f1 Mon Sep 17 00:00:00 2001 From: mastercoms Date: Fri, 18 Oct 2019 16:39:53 -0400 Subject: [PATCH 2/3] add python2 support --- dronekit/__init__.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index 58adbe8e6..f3ecc4688 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -32,7 +32,9 @@ ---- """ -import asyncio +import sys +if sys.version_info >= (3, 7): + import asyncio import collections import copy import logging @@ -640,11 +642,15 @@ def notify_attribute_listeners(self, attr_name, value, cache=False): return self._attribute_cache[attr_name] = value + loop = None + # Notify observers. for fn in self._attribute_listeners.get(attr_name, []): try: - if asyncio.iscoroutinefunction(fn): - asyncio.run(fn(self, attr_name, value)) + if sys.version_info >= (3, 5) and asyncio.iscoroutinefunction(fn): + if loop is None: + loop = asyncio.new_event_loop() + loop.run_until_complete(fn(self, attr_name, value)) else: fn(self, attr_name, value) except Exception: @@ -652,13 +658,18 @@ def notify_attribute_listeners(self, attr_name, value, cache=False): for fn in self._attribute_listeners.get('*', []): try: - if asyncio.iscoroutinefunction(fn): - asyncio.run(fn(self, attr_name, value)) + if sys.version_info >= (3, 5) and asyncio.iscoroutinefunction(fn): + if loop is None: + loop = asyncio.new_event_loop() + loop.run_until_complete(fn(self, attr_name, value)) else: fn(self, attr_name, value) except Exception: self._logger.exception('Exception in attribute handler for %s' % attr_name, exc_info=True) + if loop is not None: + loop.close() + def on_attribute(self, name): """ Decorator for attribute listeners. From e1bd1e5698f2c731665a0d329aaf70b87c8ea081 Mon Sep 17 00:00:00 2001 From: mastercoms Date: Sat, 19 Oct 2019 09:18:00 -0400 Subject: [PATCH 3/3] fix version check. asyncio was added in 3.4 --- dronekit/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index f3ecc4688..bb8d3a299 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -33,7 +33,7 @@ """ import sys -if sys.version_info >= (3, 7): +if sys.version_info >= (3, 4): import asyncio import collections import copy @@ -647,7 +647,7 @@ def notify_attribute_listeners(self, attr_name, value, cache=False): # Notify observers. for fn in self._attribute_listeners.get(attr_name, []): try: - if sys.version_info >= (3, 5) and asyncio.iscoroutinefunction(fn): + if sys.version_info >= (3, 4) and asyncio.iscoroutinefunction(fn): if loop is None: loop = asyncio.new_event_loop() loop.run_until_complete(fn(self, attr_name, value)) @@ -658,7 +658,7 @@ def notify_attribute_listeners(self, attr_name, value, cache=False): for fn in self._attribute_listeners.get('*', []): try: - if sys.version_info >= (3, 5) and asyncio.iscoroutinefunction(fn): + if sys.version_info >= (3, 4) and asyncio.iscoroutinefunction(fn): if loop is None: loop = asyncio.new_event_loop() loop.run_until_complete(fn(self, attr_name, value))