Skip to content

Commit 2f05972

Browse files
committed
Add warning to ThreadPoolExecutor docs
See python#86128 (comment) for an example where this happens. Essentially, even if it looks like you added an `atexit` handler to instruct your thread to exit gracefully, it will only be executed _after_ your thread has finished. For long-running threads (e.g. threads listening to a queue), that may never happen at all. Elsewhere in python#86128, it's recommended that `ThreadPoolExecutor` not be used for long-running tasks, but this was not reflected in the documentation. Based solely on the API, there is no reason to think you shouldn't use it for long-running tasks. The only reason appears to be a limitation in its implementation, so that should be made explicit in the docs.
1 parent 2702e40 commit 2f05972

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Doc/library/concurrent.futures.rst

+7
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ And::
149149
An :class:`Executor` subclass that uses a pool of at most *max_workers*
150150
threads to execute calls asynchronously.
151151

152+
All threads enqueued to ``ThreadPoolExecutor`` will be joined before the
153+
interpreter can exit. Note that the exit handler which does this is
154+
executed *before* any exit handlers added using `atexit`. This means
155+
exceptions in the main thread must be caught and handled in order to
156+
signal threads to exit gracefully. For this reason, it is recommended
157+
that ``ThreadPoolExecutor`` not be used for long-running tasks.
158+
152159
*initializer* is an optional callable that is called at the start of
153160
each worker thread; *initargs* is a tuple of arguments passed to the
154161
initializer. Should *initializer* raise an exception, all currently

0 commit comments

Comments
 (0)