Browse code

fix imports in runner.py

Bas Nijholt authored on 09/12/2017 02:01:30
Showing 1 changed files
... ...
@@ -2,8 +2,17 @@
2 2
 import asyncio
3 3
 import concurrent.futures as concurrent
4 4
 
5
-import distributed
6
-import ipyparallel
5
+try:
6
+    import ipyparallel
7
+    with_ipyparallel = True
8
+except ModuleNotFoundError:
9
+    with_ipyparallel = False
10
+
11
+try:
12
+    import distributed
13
+    with_distributed = True
14
+except ModuleNotFoundError:
15
+    with_distributed = False
7 16
 
8 17
 
9 18
 class Runner:
... ...
@@ -130,12 +139,12 @@ def ensure_async_executor(executor, ioloop):
130 139
         executor = concurrent.ProcessPoolExecutor()
131 140
     elif isinstance(executor, concurrent.Executor):
132 141
         pass
133
-    elif isinstance(executor, ipyparallel.Client):
142
+    elif with_ipyparallel and isinstance(executor, ipyparallel.Client):
134 143
         executor = executor.executor()
135
-    elif isinstance(executor, distributed.Client):
144
+    elif with_distributed and isinstance(executor, distributed.Client):
136 145
         executor = executor.get_executor()
137 146
     else:
138
-        raise TypeError('Only concurrent.futures.Executor, distributed.Client,'
147
+        raise TypeError('Only a concurrent.futures.Executor, distributed.Client,'
139 148
                         ' or ipyparallel.Client can be used.')
140 149
 
141 150
     return _AsyncExecutor(executor, ioloop)
... ...
@@ -179,14 +188,14 @@ class _AsyncExecutor:
179 188
     @property
180 189
     def ncores(self):
181 190
         ex = self.executor
182
-        if isinstance(ex, ipyparallel.client.view.ViewExecutor):
191
+        if with_ipyparallel and isinstance(ex, ipyparallel.client.view.ViewExecutor):
183 192
             return len(ex.view)
184 193
         elif isinstance(ex, (concurrent.ProcessPoolExecutor,
185 194
                              concurrent.ThreadPoolExecutor)):
186 195
             return ex._max_workers  # not public API!
187 196
         elif isinstance(ex, SequentialExecutor):
188 197
             return 1
189
-        elif isinstance(ex, distributed.cfexecutor.ClientExecutor):
198
+        elif with_distributed and isinstance(ex, distributed.cfexecutor.ClientExecutor):
190 199
             # XXX: check if not sum(n for n in ex._client.ncores().values())
191 200
             return len(ex._client.ncores())
192 201
         else: