Skip to content

InterpreterPoolExecutor does not work as replacement of ThreadPoolExecutor. #136603

Open
@corona10

Description

@corona10

According to PEP-734, it should work as same as ThreadPoolExecutor, but it looks not.

Code

import concurrent.futures
from concurrent.futures import InterpreterPoolExecutor, ThreadPoolExecutor


def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

with InterpreterPoolExecutor(max_workers=5) as executor:
    futures = {executor.submit(fib, n): n for n in range(10)}
    for future in concurrent.futures.as_completed(futures):
        n = futures[future]
        data = future.result()
        print(f"fib({n}): {data}")

Execution (InterpreterPoolExecutor)

Exception: AttributeError: module '__main__' has no attribute 'fib'

The above exception was the direct cause of the following exception:

concurrent.interpreters.NotShareableError: object could not be unpickled

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/xxxx/oss/cpython/op.py", line 14, in <module>
    data = future.result()
  File "/Users/xxxx/oss/cpython/Lib/concurrent/futures/_base.py", line 443, in result
    return self.__get_result()
           ~~~~~~~~~~~~~~~~~^^
  File "/Users/xxxx/oss/cpython/Lib/concurrent/futures/_base.py", line 395, in __get_result
    raise self._exception
  File "/Users/xxxx/oss/cpython/Lib/concurrent/futures/thread.py", line 86, in run
    result = ctx.run(self.task)
  File "/Users/denny.na/oss/cpython/Lib/concurrent/futures/interpreter.py", line 85, in run
    return self.interp.call(do_call, self.results, *task)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxx/oss/cpython/Lib/concurrent/interpreters/__init__.py", line 233, in call
    return self._call(callable, args, kwargs)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxx/oss/cpython/Lib/concurrent/interpreters/__init__.py", line 215, in _call
    res, excinfo = _interpreters.call(self._id, callable, args, kwargs, restrict=True)
                   ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
concurrent.interpreters.NotShareableError: args not shareable

Execution (ThreadPoolExecutor)

fib(0): 0
fib(7): 13
fib(2): 1
fib(6): 8
fib(1): 1
fib(4): 3
fib(3): 2
fib(5): 5
fib(9): 34
fib(8): 21

Metadata

Metadata

Labels

3.14bugs and security fixesstdlibPython modules in the Lib dirtopic-subinterpreterstype-bugAn unexpected behavior, bug, or error

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions