Skip to content

Commit 2b20136

Browse files
authored
Fix asyncio.to_thread() documented return type (GH-20547)
When I wrote the documentation for `asyncio.to_thread()`, I mistakenly assumed that `return await loop.run_in_executor(...)` within an async def function would return a Future. In reality, it returns a coroutine. This likely won't affect typical usage of `asyncio.to_thread()`, but it's important for the documentation to be correct here. In general, we also tend to avoid returning futures from high-level APIs in asyncio.
1 parent 007bb06 commit 2b20136

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

Doc/library/asyncio-task.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,7 @@ Running in Threads
614614
allowing context variables from the event loop thread to be accessed in the
615615
separate thread.
616616

617-
Return an :class:`asyncio.Future` which represents the eventual result of
618-
*func*.
617+
Return a coroutine that can be awaited to get the eventual result of *func*.
619618

620619
This coroutine function is primarily intended to be used for executing
621620
IO-bound functions/methods that would otherwise block the event loop if

Lib/asyncio/threads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def to_thread(func, /, *args, **kwargs):
1717
allowing context variables from the main thread to be accessed in the
1818
separate thread.
1919
20-
Return an asyncio.Future which represents the eventual result of *func*.
20+
Return a coroutine that can be awaited to get the eventual result of *func*.
2121
"""
2222
loop = events.get_running_loop()
2323
ctx = contextvars.copy_context()

0 commit comments

Comments
 (0)