Message412422
Currently, decorating a coroutine with cached_property would cache the coroutine itself. But this is not useful in any way since a coroutine cannot be awaited multiple times.
Running this code:
import asyncio
import functools
class A:
@functools.cached_property
async def hello(self):
return 'yo'
async def main():
a = A()
print(await a.hello)
print(await a.hello)
asyncio.run(main())
produces:
yo
Traceback (most recent call last):
File "t.py", line 15, in <module>
asyncio.run(main())
File "/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
return future.result()
File "t.py", line 12, in main
print(await a.hello)
RuntimeError: cannot reuse already awaited coroutine
The third-party cached_property package, on the other hand, detects a coroutine and caches its result instead. I feel this is a more useful behaviour. https://github.com/pydanny/cached-property/issues/85 |
|
Date |
User |
Action |
Args |
2022-02-03 07:28:28 | uranusjr | set | recipients:
+ uranusjr |
2022-02-03 07:28:28 | uranusjr | set | messageid: <1643873308.65.0.92890586202.issue46622@roundup.psfhosted.org> |
2022-02-03 07:28:28 | uranusjr | link | issue46622 messages |
2022-02-03 07:28:28 | uranusjr | create | |
|