diff --git a/dronekit/__init__.py b/dronekit/__init__.py index 4f3613906..bb8d3a299 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -32,6 +32,9 @@ ---- """ +import sys +if sys.version_info >= (3, 4): + import asyncio import collections import copy import logging @@ -639,19 +642,34 @@ 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: - fn(self, attr_name, value) + 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)) + 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 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)) + 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.