ThreadPoolExecutor - callback guaranteed to run in same thread as the submitted function?
pgollakota at gmail.com
Wed Sep 24 16:44:06 CEST 2014
In the ThreadPoolExecutor (TPE), is the callback always guaranteed to run in the same thread as the submitted function?
For example, I tested this with the following code. I ran it many times and it seemed like `func` and `callback` always ran in the same thread.
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
x = future.result()
cur_thread = threading.current_thread().name
if (cur_thread != x):
print('main thread: %s' % threading.current_thread())
for i in range(10000):
future = executor.submit(func, i)
However, it seemed to fail when I removed the `time.sleep(random.random())` statements, i.e. at least a few `func` functions and `callbacks` **did not** run in the same thread.
For a project that I am working on, the callback must always run on the same thread as the submitted function, so I wanted to be sure that this is guaranteed by TPE. (And also the results of the test without the random sleep seemed puzzling).
I looked at the source code for executors (see https://hg.python.org/cpython/file/6d44906344f4/Lib/concurrent/futures/_base.py#l297) and it does not seem like we switch the thread to the main thread before we run the callback. But just wanted to be sure.
Any insight would be appreciated!
More information about the Python-list