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

How to add cache in a async function? #198

Open
vba34520 opened this issue Sep 21, 2020 · 0 comments · May be fixed by #206
Open

How to add cache in a async function? #198

vba34520 opened this issue Sep 21, 2020 · 0 comments · May be fixed by #206

Comments

@vba34520
Copy link

How to add cache in a async function?

import asyncio
from pathlib import Path
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options

cache_opts = {
    'cache.type': 'file',
    'cache.data_dir': Path(__file__).parent / 'cache/data',
    'cache.lock_dir': Path(__file__).parent / 'cache/lock'
}

cache = CacheManager(**parse_cache_config_options(cache_opts))


# @cache.cache('fib', type='file', expire=60*5)
async def fib(n):
    if n < 2:
        return n
    return await fib(n - 1) + await fib(n - 2)


async def main():
    result = [await fib(i) for i in range(35)]
    print(result)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

It caused RuntimeWarning: coroutine 'fib' was never awaited.

Looking for your reply! Thank you.

@Skeen Skeen linked a pull request Feb 11, 2021 that will close this issue
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 a pull request may close this issue.

1 participant