2

When I run the following python code:

import asyncio
import logging
logging.basicConfig(level=logging.DEBUG)

async def read_future(fut):
    print(await fut)

async def write_future(fut):
    fut.set_result('My Value')

async def main():
    loop = asyncio.get_running_loop()
    fut = loop.create_future()
    asyncio.gather(read_future(fut), write_future(fut))

asyncio.run(main(), debug=True)

Instead of read_future waiting for the result of fut to be set, the program crashes with the following error:

DEBUG:asyncio:Using selector: KqueueSelector
ERROR:asyncio:_GatheringFuture exception was never retrieved
future: <_GatheringFuture finished exception=CancelledError() created at /Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/tasks.py:642>
source_traceback: Object created at (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 566, in run_until_complete
    self.run_forever()
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 1763, in _run_once
    handle._run()
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "<stdin>", line 4, in main
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/tasks.py", line 766, in gather
    outer = _GatheringFuture(children, loop=loop)
  File "/Users/<user>/.pyenv/versions/3.7.4/lib/python3.7/asyncio/tasks.py", line 642, in __init__
    super().__init__(loop=loop)
concurrent.futures._base.CancelledError
DEBUG:asyncio:Close <_UnixSelectorEventLoop running=False closed=False debug=True>

What am I doing wrong in this code? I want to be able to await the Future fut and continue after the Future has a value/exception set.

Red
  • 26,798
  • 7
  • 36
  • 58
jcarrete5
  • 37
  • 2
  • 7

2 Answers2

1

Your problem is that asyncio.gather is itself async (returns an awaitable); by not awaiting it, you never handed control back to the event loop, nor did you store the awaitable, so it was immediately cleaned up, implicitly cancelling it, and by extension, all of the awaitables it controlled.

To fix, just make sure you await the results of the gather:

await asyncio.gather(read_future(fut), write_future(fut))

Try it online!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
-1

From https://docs.python.org/3/library/asyncio-future.html#asyncio.Future.result:

result()

Return the result of the Future.

If the Future is done and has a result set by the set_result() method, the result value is returned.

If the Future is done and has an exception set by the set_exception() method, this method raises the exception.

If the Future has been cancelled, this method raises a CancelledError exception.

If the Future’s result isn’t yet available, this method raises a InvalidStateError exception.

(Bold added.)

I'm not sure why the Future is getting cancelled, but that seems to be the cause of the issue.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45