cpython icon indicating copy to clipboard operation
cpython copied to clipboard

InterpreterPoolExecutor doesn't respect "thread_name_prefix"

Open paultiq opened this issue 1 year ago • 1 comments

Bug report

Bug description:

InterpreterPoolExecutor takes a thread_name_prefix parameter, but it doesn't propagate to the underlying subinterpreter.

from concurrent.futures import ThreadPoolExecutor
try:
    from concurrent.futures.interpreter import InterpreterPoolExecutor # 3.14+
except ModuleNotFoundError:
    from interpreters_backport.concurrent.futures.interpreter import InterpreterPoolExecutor 

with ThreadPoolExecutor(thread_name_prefix="test_tpe") as tpe_executor:
    tpe_executor.submit(exec, "import threading;print(threading.current_thread().name)")  # 
    
with InterpreterPoolExecutor(thread_name_prefix="test_ipe") as ipe_executor:
    ipe_executor.submit(exec, "import threading;print(threading.current_thread().name)")

Produces

test_tpe_0
Dummy-1

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

paultiq avatar Oct 24 '24 12:10 paultiq

Confirmed on the main branch, but this isn't related to InterpreterPoolExecutor. ipe_executor._thread_name_prefix does appear to be set to the correct value, and the Thread object name is the right value when checking it directly with ipe_executor._threads.

The problem is that thread names aren't pushed down to subinterpreters, which very well might be a wontfix depending on the complexity of the change. Here's a reproducer for that:

import _interpreters
import threading

source = """
import threading

print(threading.current_thread().name)  # Dummy-1
"""

def main():
    interp = _interpreters.create()
    print(threading.current_thread().name)  # Hello
    _interpreters.run_string(interp, source)

threading.Thread(target=main, name="hello").start()

cc @ericsnowcurrently, it might be better to just note this when subinterpreters get documented better. (Though, if that's the case, thread_name_prefix should just get removed from InterpreterPoolExecutor.)

ZeroIntensity avatar Oct 24 '24 13:10 ZeroIntensity