This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: asyncio example uses non-existing/documented method
Type: Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7, Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: asvetlov, docs@python, giampaolo.rodola, hfingler, xtreak, yselivanov
Priority: normal Keywords:

Created on 2018-03-21 21:06 by hfingler, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg314223 - (view) Author: Henrique Fingler (hfingler) Date: 2018-03-21 21:06
In the documentation of asyncio.run_coroutine_threadsafe(coro, loop), in Section (, the example code does the following:

    future = asyncio.run_coroutine_threadsafe(coro, loop)
    # Wait for the result with an optional timeout argument
    assert future.result(timeout) == 3

 The problem is that the result method of a future, according to the documentation doesn't take parameters. It's in Section (

        Return the result this future represents.

 The same function is used in Section (

 This error is present in all Python 3.* docs.

 From the asyncio source code (, we have this in the Future class definition:

class Future:
    """This class is *almost* compatible with concurrent.futures.Future.
    - This class is not thread-safe.
    - result() and exception() do not take a timeout argument and
      raise an exception when the future isn't done yet.


So this example needs to be reworked, I'd do it if I knew more about asyncio. My ideas involve either using a add_done_callback with a flag or just busy waiting until future.done().
msg325867 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-20 12:57
I think `asyncio.run_coroutine_threadsafe(coro, loop)` returns a concurrent.futures.Future [1] which supports timeout and not asyncio.Future which doesn't support timeout. Please add in if I am missing anything. Since asyncio docs are being rewritten this would be a great time to contribute too if you would like to add more clarification.

Docs at [1] also say the below : 

asyncio.run_coroutine_threadsafe(coro, loop)

    Submit a coroutine to the given event loop. Thread-safe.

    Return a concurrent.futures.Future to wait for the result from another OS thread.

# This needs to be called from a different thread as docs said but I am using future.result(timeout) just to make sure there is no error with respect to function argument.

import asyncio

loop = asyncio.get_event_loop()
timeout = 4

# Create a coroutine
coro = asyncio.sleep(1, result=3)

# Submit the coroutine to a given loop
future = asyncio.run_coroutine_threadsafe(coro, loop)

# Wait for the result with an optional timeout argument
assert future.result(timeout) == 3

➜  cpython git:(master) ./python.exe
Traceback (most recent call last):
  File "../", line 13, in <module>
    assert future.result(timeout) == 3
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/concurrent/futures/", line 438, in result
    raise TimeoutError()


msg326795 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-10-01 11:39
@xtreak is right, `run_coroutine_threadsafe()` returns `concurrent.futures.Future` object.

Personally, I like the fact that `fut.result()` is called with timeout parameter to reflect the fact of the different object type.
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77298
2018-10-01 11:39:31asvetlovsetstatus: open -> closed
resolution: not a bug
messages: + msg326795

stage: resolved
2018-09-20 12:57:47xtreaksetnosy: + xtreak
messages: + msg325867
2018-03-24 00:07:46terry.reedysetversions: - Python 3.5
2018-03-22 02:05:34ned.deilysetnosy: + giampaolo.rodola, asvetlov, yselivanov
2018-03-21 21:06:46hfinglercreate