Skip to content

How to add cache in a async function? #198

@vba34520

Description

@vba34520

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions