Skip to content

Commit 4bc9ac4

Browse files
committed
Merge branch 'set_default_executor'
set a different default executor for Windows and Unix systems Closes #49 See merge request qt/adaptive!52
2 parents 60b2303 + 60be696 commit 4bc9ac4

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

adaptive/runner.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -*- coding: utf-8 -*-
22
import asyncio
3+
import concurrent.futures as concurrent
34
import functools
45
import inspect
5-
import concurrent.futures as concurrent
6-
import warnings
6+
import os
77
import time
8+
import warnings
89

910
from .notebook_integration import live_plot, live_info, in_ipynb
1011

@@ -27,6 +28,31 @@
2728
pass
2829

2930

31+
if os.name == 'nt':
32+
if with_distributed:
33+
_default_executor = distributed.Client
34+
_default_executor_kwargs = {'address': distributed.LocalCluster()}
35+
else:
36+
_windows_executor_msg = (
37+
"The default executor on Windows for 'adaptive.Runner' cannot "
38+
"be used because the package 'distributed' is not installed. "
39+
"Either install 'distributed' or explicitly specify an executor "
40+
"when using 'adaptive.Runner'."
41+
)
42+
43+
_default_executor_kwargs = {}
44+
45+
def _default_executor(*args, **kwargs):
46+
raise RuntimeError(_windows_executor_msg)
47+
48+
warnings.warn(_windows_executor_msg)
49+
50+
else:
51+
_default_executor = concurrent.ProcessPoolExecutor
52+
_default_executor_kwargs = {}
53+
54+
55+
3056
class BaseRunner:
3157
"""Base class for runners that use concurrent.futures.Executors.
3258
@@ -86,9 +112,12 @@ class BlockingRunner(BaseRunner):
86112
The end condition for the calculation. This function must take
87113
the learner as its sole argument, and return True when we should
88114
stop requesting more points.
89-
executor : concurrent.futures.Executor, or ipyparallel.Client, optional
115+
executor : concurrent.futures.Executor, distributed.Client,
116+
or ipyparallel.Client, optional
90117
The executor in which to evaluate the function to be learned.
91-
If not provided, a new ProcessPoolExecutor is used.
118+
If not provided, a new `ProcessPoolExecutor` is used on Unix systems
119+
while on Windows a `distributed.Client` is used if `distributed` is
120+
installed.
92121
ntasks : int, optional
93122
The number of concurrent function evaluations. Defaults to the number
94123
of cores available in 'executor'.
@@ -183,9 +212,12 @@ class AsyncRunner(BaseRunner):
183212
the learner as its sole argument, and return True when we should
184213
stop requesting more points. If not provided, the runner will run
185214
forever, or until 'self.task.cancel()' is called.
186-
executor : concurrent.futures.Executor, or ipyparallel.Client, optional
215+
executor : concurrent.futures.Executor, distributed.Client,
216+
or ipyparallel.Client, optional
187217
The executor in which to evaluate the function to be learned.
188-
If not provided, a new ProcessPoolExecutor is used.
218+
If not provided, a new `ProcessPoolExecutor` is used on Unix systems
219+
while on Windows a `distributed.Client` is used if `distributed` is
220+
installed.
189221
ntasks : int, optional
190222
The number of concurrent function evaluations. Defaults to the number
191223
of cores available in 'executor'.
@@ -440,8 +472,9 @@ def shutdown(self, wait=True):
440472

441473
def _ensure_executor(executor):
442474
if executor is None:
443-
return concurrent.ProcessPoolExecutor()
444-
elif isinstance(executor, concurrent.Executor):
475+
executor = _default_executor(**_default_executor_kwargs)
476+
477+
if isinstance(executor, concurrent.Executor):
445478
return executor
446479
elif with_ipyparallel and isinstance(executor, ipyparallel.Client):
447480
return executor.executor()

0 commit comments

Comments
 (0)