From 97797abb518f6b73f4165206bac73e770f4d1aa3 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sun, 25 Jan 2015 11:08:57 -0800 Subject: [PATCH] Use importutils.try_import for optional eventlet imports Instead of doing try: except ImportError for eventlet imports use the oslo.utils importutils.try_import function that safely does the same with less verbosity for the optional usage of eventlet imports/code that we have. Change-Id: I7eaa7c5908ffb04282892c9f6af04044b73f4f8c --- taskflow/types/futures.py | 12 +++++------- taskflow/utils/async_utils.py | 6 ++---- taskflow/utils/eventlet_utils.py | 10 +++++----- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/taskflow/types/futures.py b/taskflow/types/futures.py index 128f9aa5..1d847ddc 100644 --- a/taskflow/types/futures.py +++ b/taskflow/types/futures.py @@ -20,15 +20,13 @@ import threading from concurrent import futures as _futures from concurrent.futures import process as _process from concurrent.futures import thread as _thread +from oslo_utils import importutils from oslo_utils import reflection -try: - from eventlet.green import threading as greenthreading - from eventlet import greenpool - from eventlet import patcher as greenpatcher - from eventlet import queue as greenqueue -except ImportError: - pass +greenpatcher = importutils.try_import('eventlet.patcher') +greenpool = importutils.try_import('eventlet.greenpool') +greenqueue = importutils.try_import('eventlet.queue') +greenthreading = importutils.try_import('eventlet.green.threading') from taskflow.types import timing from taskflow.utils import eventlet_utils as eu diff --git a/taskflow/utils/async_utils.py b/taskflow/utils/async_utils.py index 71eafa18..e04c44e7 100644 --- a/taskflow/utils/async_utils.py +++ b/taskflow/utils/async_utils.py @@ -16,11 +16,9 @@ from concurrent import futures as _futures from concurrent.futures import _base +from oslo_utils import importutils -try: - from eventlet.green import threading as greenthreading -except ImportError: - pass +greenthreading = importutils.try_import('eventlet.green.threading') from taskflow.types import futures from taskflow.utils import eventlet_utils as eu diff --git a/taskflow/utils/eventlet_utils.py b/taskflow/utils/eventlet_utils.py index 68dd355a..2f5a42a6 100644 --- a/taskflow/utils/eventlet_utils.py +++ b/taskflow/utils/eventlet_utils.py @@ -14,11 +14,11 @@ # License for the specific language governing permissions and limitations # under the License. -try: - import eventlet as _eventlet # noqa - EVENTLET_AVAILABLE = True -except ImportError: - EVENTLET_AVAILABLE = False +from oslo_utils import importutils + +_eventlet = importutils.try_import('eventlet') + +EVENTLET_AVAILABLE = bool(_eventlet) def check_for_eventlet(exc=None):