Skip to content

Commit fd94a42

Browse files
Drop the "shared" parameter.
1 parent 39c6768 commit fd94a42

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

Doc/library/concurrent.futures.rst

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ efficient alternative is to serialize with :mod:`pickle` and then send
287287
the bytes over a shared :mod:`socket <socket>` or
288288
:func:`pipe <os.pipe>`.
289289

290-
.. class:: InterpreterPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=(), shared=None)
290+
.. class:: InterpreterPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=())
291291

292292
A :class:`ThreadPoolExecutor` subclass that executes calls asynchronously
293293
using a pool of at most *max_workers* threads. Each thread runs
@@ -308,13 +308,6 @@ the bytes over a shared :mod:`socket <socket>` or
308308
The executor may replace uncaught exceptions from *initializer*
309309
with :class:`~concurrent.futures.interpreter.ExecutionFailed`.
310310

311-
The optional *shared* argument is a :class:`dict` of objects that all
312-
interpreters in the pool share. The *shared* items are added to each
313-
interpreter's ``__main__`` module. Not all objects are shareable.
314-
Shareable objects include the builtin singletons, :class:`str`
315-
and :class:`bytes`, and :class:`memoryview`. See :pep:`734`
316-
for more info.
317-
318311
Other caveats from parent :class:`ThreadPoolExecutor` apply here.
319312

320313
:meth:`~Executor.submit` and :meth:`~Executor.map` work like normal,

Lib/concurrent/futures/interpreter.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __str__(self):
3939
class WorkerContext(_thread.WorkerContext):
4040

4141
@classmethod
42-
def prepare(cls, initializer, initargs, shared):
42+
def prepare(cls, initializer, initargs):
4343
def resolve_task(fn, args, kwargs):
4444
if isinstance(fn, str):
4545
# XXX Circle back to this later.
@@ -58,12 +58,11 @@ def resolve_task(fn, args, kwargs):
5858
else:
5959
initdata = None
6060
def create_context():
61-
return cls(initdata, shared)
61+
return cls(initdata)
6262
return create_context, resolve_task
6363

64-
def __init__(self, initdata, shared=None):
64+
def __init__(self, initdata):
6565
self.initdata = initdata
66-
self.shared = dict(shared) if shared else None
6766
self.interpid = None
6867
self.resultsid = None
6968

@@ -131,10 +130,6 @@ def initialize(self):
131130
maxsize = 0
132131
self.resultsid = _interpqueues.create(maxsize)
133132

134-
if self.shared:
135-
_interpreters.set___main___attrs(
136-
self.interpid, self.shared, restrict=True)
137-
138133
if self.initdata:
139134
self.run(self.initdata)
140135
except BaseException:
@@ -180,11 +175,11 @@ class InterpreterPoolExecutor(_thread.ThreadPoolExecutor):
180175
BROKEN = BrokenInterpreterPool
181176

182177
@classmethod
183-
def prepare_context(cls, initializer, initargs, shared):
184-
return WorkerContext.prepare(initializer, initargs, shared)
178+
def prepare_context(cls, initializer, initargs):
179+
return WorkerContext.prepare(initializer, initargs)
185180

186181
def __init__(self, max_workers=None, thread_name_prefix='',
187-
initializer=None, initargs=(), shared=None):
182+
initializer=None, initargs=()):
188183
"""Initializes a new InterpreterPoolExecutor instance.
189184
190185
Args:
@@ -194,8 +189,6 @@ def __init__(self, max_workers=None, thread_name_prefix='',
194189
initializer: A callable or script used to initialize
195190
each worker interpreter.
196191
initargs: A tuple of arguments to pass to the initializer.
197-
shared: A mapping of shareabled objects to be inserted into
198-
each worker interpreter.
199192
"""
200193
super().__init__(max_workers, thread_name_prefix,
201-
initializer, initargs, shared=shared)
194+
initializer, initargs)

Lib/test/test_concurrent_futures/test_interpreter_pool.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,6 @@ def initializer(self):
224224
with self.assertRaises(BrokenInterpreterPool):
225225
fut.result()
226226

227-
def test_init_shared(self):
228-
msg = b'eggs'
229-
r, w = self.pipe()
230-
script = f"""if True:
231-
import os
232-
if __name__ != '__main__':
233-
import __main__
234-
spam = __main__.spam
235-
os.write({w}, spam + b'\\0')
236-
"""
237-
238-
executor = self.executor_type(shared={'spam': msg})
239-
fut = executor.submit(exec, script)
240-
fut.result()
241-
after = read_msg(r)
242-
243-
self.assertEqual(after, msg)
244-
245227
@unittest.expectedFailure
246228
def test_init_exception_in_script(self):
247229
executor = self.executor_type(initializer='raise Exception("spam")')
@@ -363,16 +345,39 @@ def test_submit_exception_in_func(self):
363345

364346
def test_saturation(self):
365347
blocker = queues.create()
366-
executor = self.executor_type(4, shared=dict(blocker=blocker))
348+
executor = self.executor_type(4)
367349

368350
for i in range(15 * executor._max_workers):
369-
executor.submit(exec, 'import __main__; __main__.blocker.get()')
370-
#executor.submit('blocker.get()')
351+
executor.submit(blocker.get)
371352
self.assertEqual(len(executor._threads), executor._max_workers)
372353
for i in range(15 * executor._max_workers):
373354
blocker.put_nowait(None)
374355
executor.shutdown(wait=True)
375356

357+
def test_blocking(self):
358+
ready = queues.create()
359+
blocker = queues.create()
360+
361+
def run(ready, blocker):
362+
ready.put(None)
363+
blocker.get() # blocking
364+
365+
numtasks = 10
366+
futures = []
367+
executor = self.executor_type()
368+
try:
369+
for i in range(numtasks):
370+
fut = executor.submit(run, ready, blocker)
371+
futures.append(fut)
372+
# Wait for them all to be ready.
373+
for i in range(numtasks):
374+
ready.get() # blocking
375+
# Unblock the workers.
376+
for i in range(numtasks):
377+
blocker.put_nowait(None)
378+
finally:
379+
executor.shutdown(wait=True)
380+
376381
@support.requires_gil_enabled("gh-117344: test is flaky without the GIL")
377382
def test_idle_thread_reuse(self):
378383
executor = self.executor_type()

0 commit comments

Comments
 (0)