Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove improper usage of @cache on async functions #67

Merged
merged 2 commits into from
Apr 19, 2023

Conversation

gotmax23
Copy link
Collaborator

Neither functools.cache nor functools.lru_cache which was previously
used here works properly with async functions. These decorators cache
the coroutine, but coroutines can only be awaited once, so this falls
apart.

Take the following example:

In [1]: from functools import cache

In [2]: @cache
   ...: async def abc():
   ...:     return 1234
   ...:

In [3]: print(await abc())
1234

In [4]: print(await abc())
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 1
----> 1 print(await abc())

RuntimeError: cannot reuse already awaited coroutine

Neither functools.cache nor functools.lru_cache which was previously
used here works properly with async functions. These decorators cache
the coroutine, but coroutines can only be awaited once, so this falls
apart.

Take the following example:

```
In [1]: from functools import cache

In [2]: @cache
   ...: async def abc():
   ...:     return 1234
   ...:

In [3]: print(await abc())
1234

In [4]: print(await abc())
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 1
----> 1 print(await abc())

RuntimeError: cannot reuse already awaited coroutine
```
@gotmax23
Copy link
Collaborator Author

This should be backported to stable-1 IMO, but it'll have to be done manually

@felixfontein felixfontein merged commit d9c78db into ansible-community:main Apr 19, 2023
@felixfontein
Copy link
Collaborator

Thanks for doing this!

gotmax23 added a commit to gotmax23/antsibull-core that referenced this pull request Apr 19, 2023
)

* remove improper usage of @cache on async functions

Neither functools.cache nor functools.lru_cache which was previously
used here works properly with async functions. These decorators cache
the coroutine, but coroutines can only be awaited once, so this falls
apart.

Take the following example:

```
In [1]: from functools import cache

In [2]: @cache
   ...: async def abc():
   ...:     return 1234
   ...:

In [3]: print(await abc())
1234

In [4]: print(await abc())
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 1
----> 1 print(await abc())

RuntimeError: cannot reuse already awaited coroutine
```

* Fix changelog.

---------

Co-authored-by: Felix Fontein <[email protected]>
felixfontein added a commit that referenced this pull request Apr 19, 2023
)

* remove improper usage of @cache on async functions (#67)

* remove improper usage of @cache on async functions

Neither functools.cache nor functools.lru_cache which was previously
used here works properly with async functions. These decorators cache
the coroutine, but coroutines can only be awaited once, so this falls
apart.

Take the following example:

```
In [1]: from functools import cache

In [2]: @cache
   ...: async def abc():
   ...:     return 1234
   ...:

In [3]: print(await abc())
1234

In [4]: print(await abc())
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 1
----> 1 print(await abc())

RuntimeError: cannot reuse already awaited coroutine
```

* Fix changelog.

---------

Co-authored-by: Felix Fontein <[email protected]>

* update changelog entry

---------

Co-authored-by: Felix Fontein <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants