Thomas Grainger <tagr...@gmail.com> added the comment:

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
```

----------
nosy: +graingert

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44963>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to