Skip to content

Commit 6dd6b65

Browse files
committed
gh-141617: Clarify ThreadPoolExecutor deadlock example in docs
1 parent 2a5ba3e commit 6dd6b65

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Doc/library/concurrent.futures.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ Executor Objects
4040
with ThreadPoolExecutor(max_workers=1) as executor:
4141
future = executor.submit(pow, 323, 1235)
4242
print(future.result())
43-
43+
# Note: calling future.result() outside this with statement would work fine,
44+
# but calling it here is safe because we print it immediately and the executor
45+
# can complete the task before the context manager exits.
4446
.. method:: map(fn, *iterables, timeout=None, chunksize=1, buffersize=None)
4547

4648
Similar to :func:`map(fn, *iterables) <map>` except:
@@ -152,7 +154,9 @@ And::
152154

153155
executor = ThreadPoolExecutor(max_workers=1)
154156
executor.submit(wait_on_future).result()
155-
157+
# Note: calling future.result() here would cause a deadlock because the single
158+
# worker thread is already executing wait_on_future(), which itself is waiting
159+
# for a result from the same executor.
156160

157161
.. class:: ThreadPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=())
158162

0 commit comments

Comments
 (0)