]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix asyncio.to_thread() documented return type (GH-20547)
authorKyle Stanley <aeros167@gmail.com>
Sun, 31 May 2020 07:07:04 +0000 (03:07 -0400)
committerGitHub <noreply@github.com>
Sun, 31 May 2020 07:07:04 +0000 (00:07 -0700)
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.

Doc/library/asyncio-task.rst
Lib/asyncio/threads.py

index 847363b134a7a0222ad9e26b541bfc237ace9742..21824ca537f77fc983b2564d39ccca572d7efb6e 100644 (file)
@@ -614,8 +614,7 @@ Running in Threads
    allowing context variables from the event loop thread to be accessed in the
    separate thread.
 
-   Return an :class:`asyncio.Future` which represents the eventual result of
-   *func*.
+   Return a coroutine that can be awaited to get the eventual result of *func*.
 
    This coroutine function is primarily intended to be used for executing
    IO-bound functions/methods that would otherwise block the event loop if
index 51e0ba95d822e5e9626bc2e6b91670285a9d7206..34b7513a42090298a69a92858e8deb91322c6bfb 100644 (file)
@@ -17,7 +17,7 @@ async def to_thread(func, /, *args, **kwargs):
     allowing context variables from the main thread to be accessed in the
     separate thread.
 
-    Return an asyncio.Future which represents the eventual result of *func*.
+    Return a coroutine that can be awaited to get the eventual result of *func*.
     """
     loop = events.get_running_loop()
     ctx = contextvars.copy_context()