-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-124694: Add concurrent.futures.InterpreterPoolExecutor #124548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
5c69d38
01789be
6def4be
84993a5
c540cf0
45d584d
c90c016
1cb4657
4dc0989
57b2db6
75e11d2
69c2b8e
f03c314
4806d9f
efc0395
a29aee3
8bab457
cd29914
0287f3b
80cd7b1
f8d4273
3a8bfce
af6c27a
8c0a405
1ae7ca2
d24e85d
05a03ad
f150931
97d0292
baf0504
5c3a327
a2032a8
54119b8
744dca7
f61d62d
ee65bb2
a7f5c50
b148e09
e365ae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
"""Implements InterpreterPoolExecutor.""" | ||
|
||
import contextlib | ||
import pickle | ||
import textwrap | ||
from . import thread as _thread | ||
import _interpreters | ||
import _interpqueues | ||
|
||
|
||
class ExecutionFailed(_interpreters.InterpreterError): | ||
"""An unhandled exception happened during execution.""" | ||
|
||
def __init__(self, excinfo): | ||
msg = excinfo.formatted | ||
if not msg: | ||
if excinfo.type and excinfo.msg: | ||
msg = f'{excinfo.type.__name__}: {excinfo.msg}' | ||
else: | ||
msg = excinfo.type.__name__ or excinfo.msg | ||
super().__init__(msg) | ||
self.excinfo = excinfo | ||
|
||
def __str__(self): | ||
try: | ||
formatted = self.excinfo.errdisplay | ||
except Exception: | ||
return super().__str__() | ||
else: | ||
return textwrap.dedent(f""" | ||
{super().__str__()} | ||
|
||
Uncaught in the interpreter: | ||
|
||
{formatted} | ||
""".strip()) | ||
|
||
|
||
UNBOUND = 2 # error; this should not happen. | ||
|
||
|
||
class WorkerContext(_thread.WorkerContext): | ||
|
||
@classmethod | ||
def prepare(cls, initializer, initargs, shared): | ||
def resolve_task(fn, args, kwargs): | ||
if isinstance(fn, str): | ||
if args or kwargs: | ||
raise ValueError(f'a script does not take args or kwargs, got {args!r} and {kwargs!r}') | ||
data = fn | ||
kind = 'script' | ||
# Make sure the script compiles. | ||
# XXX Keep the compiled code object? | ||
|
||
compile(fn, '<string>', 'exec') | ||
else: | ||
# XXX This does not work if fn comes from the __main__ module. | ||
data = pickle.dumps((fn, args, kwargs)) | ||
kind = 'function' | ||
return (data, kind) | ||
|
||
if isinstance(initializer, str): | ||
if initargs: | ||
raise ValueError(f'an initializer script does not take args, got {initargs!r}') | ||
if initializer is not None: | ||
initdata = resolve_task(initializer, initargs, {}) | ||
else: | ||
initdata = None | ||
def create_context(): | ||
return cls(initdata, shared) | ||
return create_context, resolve_task | ||
|
||
@classmethod | ||
@contextlib.contextmanager | ||
def _capture_exc(cls, resultsid): | ||
try: | ||
yield | ||
except Exception as exc: | ||
err = pickle.dumps(exc) | ||
_interpqueues.put(resultsid, (None, err), 1, UNBOUND) | ||
else: | ||
_interpqueues.put(resultsid, (None, None), 0, UNBOUND) | ||
|
||
@classmethod | ||
def _call(cls, func, args, kwargs, resultsid): | ||
try: | ||
res = func(*args, **kwargs) | ||
except Exception as exc: | ||
err = pickle.dumps(exc) | ||
_interpqueues.put(resultsid, (None, err), 1, UNBOUND) | ||
else: | ||
# Send the result back. | ||
try: | ||
_interpqueues.put(resultsid, (res, None), 0, UNBOUND) | ||
except _interpreters.NotShareableError: | ||
res = pickle.dumps(res) | ||
_interpqueues.put(resultsid, (res, None), 1, UNBOUND) | ||
|
||
@classmethod | ||
def _call_pickled(cls, pickled, resultsid): | ||
fn, args, kwargs = pickle.loads(pickled) | ||
cls._call(fn, args, kwargs, resultsid) | ||
|
||
def __init__(self, initdata, shared=None): | ||
self.initdata = initdata | ||
self.shared = dict(shared) if shared else None | ||
self.interpid = None | ||
self.resultsid = None | ||
|
||
def __del__(self): | ||
if self.interpid is not None: | ||
self.finalize() | ||
|
||
def _exec(self, script): | ||
assert self.interpid is not None | ||
excinfo = _interpreters.exec(self.interpid, script, restrict=True) | ||
if excinfo is not None: | ||
raise ExecutionFailed(excinfo) | ||
|
||
def initialize(self): | ||
assert self.interpid is None, self.interpid | ||
self.interpid = _interpreters.create(reqrefs=True) | ||
try: | ||
_interpreters.incref(self.interpid) | ||
|
||
maxsize = 0 | ||
fmt = 0 | ||
self.resultsid = _interpqueues.create(maxsize, fmt, UNBOUND) | ||
|
||
initscript = f"""if True: | ||
from {__name__} import WorkerContext | ||
""" | ||
self._exec(initscript) | ||
|
||
if self.shared: | ||
_interpreters.set___main___attrs( | ||
self.interpid, self.shared, restrict=True) | ||
|
||
if self.initdata: | ||
self.run(self.initdata) | ||
except _interpreters.InterpreterNotFoundError: | ||
raise # re-raise | ||
except BaseException: | ||
self.finalize() | ||
raise # re-raise | ||
|
||
def finalize(self): | ||
interpid = self.interpid | ||
resultsid = self.resultsid | ||
self.resultsid = None | ||
self.interpid = None | ||
if resultsid is not None: | ||
try: | ||
_interpqueues.destroy(resultsid) | ||
except _interpqueues.QueueNotFoundError: | ||
pass | ||
if interpid is not None: | ||
try: | ||
_interpreters.decref(interpid) | ||
except _interpreters.InterpreterNotFoundError: | ||
pass | ||
|
||
def run(self, task): | ||
data, kind = task | ||
if kind == 'script': | ||
script = textwrap.dedent(f""" | ||
with WorkerContext._capture_exc({self.resultsid}): | ||
{{}}""") | ||
script = script.format(textwrap.indent(data, ' ')) | ||
elif kind == 'function': | ||
script = f'WorkerContext._call_pickled({data!r}, {self.resultsid})' | ||
else: | ||
raise NotImplementedError(kind) | ||
self._exec(script) | ||
|
||
# Return the result, or raise the exception. | ||
while True: | ||
try: | ||
obj = _interpqueues.get(self.resultsid) | ||
except _interpqueues.QueueNotFoundError: | ||
raise # re-raise | ||
# XXX This breaks if test.support.interpreters.queues | ||
# doesn't exist. | ||
except _interpqueues.QueueError: | ||
continue | ||
else: | ||
break | ||
(res, exc), pickled, unboundop = obj | ||
assert unboundop is None, unboundop | ||
if exc is not None: | ||
assert res is None, res | ||
assert pickled | ||
exc = pickle.loads(exc) | ||
raise exc from exc | ||
return pickle.loads(res) if pickled else res | ||
|
||
|
||
class InterpreterPoolExecutor(_thread.ThreadPoolExecutor): | ||
|
||
@classmethod | ||
def prepare_context(cls, initializer, initargs, shared): | ||
return WorkerContext.prepare(initializer, initargs, shared) | ||
|
||
def __init__(self, max_workers=None, thread_name_prefix='', | ||
initializer=None, initargs=(), shared=None): | ||
"""Initializes a new InterpreterPoolExecutor instance. | ||
|
||
Args: | ||
max_workers: The maximum number of interpreters that can be used to | ||
execute the given calls. | ||
thread_name_prefix: An optional name prefix to give our threads. | ||
initializer: A callable or script used to initialize | ||
each worker interpreter. | ||
initargs: A tuple of arguments to pass to the initializer. | ||
shared: A mapping of shareabled objects to be inserted into | ||
each worker interpreter. | ||
""" | ||
super().__init__(max_workers, thread_name_prefix, | ||
initializer, initargs, shared=shared) |
Uh oh!
There was an error while loading. Please reload this page.