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.

Author graingert
Recipients graingert, jab, lordmauve
Date 2021-08-20.20:39:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1629491945.59.0.335741131837.issue44963@roundup.psfhosted.org>
In-reply-to
Content
it also fails with asyncio.create_task

```
import asyncio

async def agen():
    yield

async def main():
    p = agen()
    await asyncio.create_task(anext(p, 'finished'))

asyncio.run(main())
```

```
Traceback (most recent call last):
  File "/home/graingert/projects/datapraxis/analysis/foo.py", line 10, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "/home/graingert/projects/asyncio-demo/foo.py", line 8, in main
    await asyncio.create_task(anext(p, 'finished'))
  File "/usr/lib/python3.10/asyncio/tasks.py", line 337, in create_task
    task = loop.create_task(coro)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 433, in create_task
    task = tasks.Task(coro, loop=self, name=name)
TypeError: a coroutine was expected, got <anext_awaitable object at 0x7f67f5587550>
```


there's also a problem with cancelling and aclosing an async gen this way:

```
import asyncio
import contextlib

async def agen():
    await asyncio.sleep(1)
    yield

async def main():
    async with contextlib.aclosing(agen()) as p:
        asyncio.current_task().cancel()
        try:
            await anext(p, 'finished')
        except asyncio.CancelledError:
            pass

asyncio.run(main())
```

```
an error occurred during closing of asynchronous generator <async_generator object agen at 0x7f2061a61dc0>
asyncgen: <async_generator object agen at 0x7f2061a61dc0>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "/home/graingert/projects/asyncio-demo/foo.py", line 9, in main
    async with contextlib.aclosing(agen()) as p:
  File "/usr/lib/python3.10/contextlib.py", line 366, in __aexit__
    await self.thing.aclose()
RuntimeError: aclose(): asynchronous generator is already running

During handling of the above exception, another exception occurred:

RuntimeError: aclose(): asynchronous generator is already running
Traceback (most recent call last):
  File "/home/graingert/projects/datapraxis/analysis/foo.py", line 16, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "/home/graingert/projects/asyncio-demo/foo.py", line 9, in main
    async with contextlib.aclosing(agen()) as p:
  File "/usr/lib/python3.10/contextlib.py", line 366, in __aexit__
    await self.thing.aclose()
RuntimeError: aclose(): asynchronous generator is already running
```
History
Date User Action Args
2021-08-20 20:39:05graingertsetrecipients: + graingert, jab, lordmauve
2021-08-20 20:39:05graingertsetmessageid: <1629491945.59.0.335741131837.issue44963@roundup.psfhosted.org>
2021-08-20 20:39:05graingertlinkissue44963 messages
2021-08-20 20:39:05graingertcreate