Skip to content

michalc/aiomemoize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aiomemoize CircleCI Test Coverage

Memoize asyncio Python calls. Invalidation is manual/explicit for each set of arguments, although exceptions raised are not cached. This can be used for coroutines, and functions that return a promise.

Installation

pip install aiomemoize

Usage

For a coroutine whose arguments are hashable, you can create a memoized version by passing it to memoize. This returns a tuple of the memoized function, and a function to invalidate the cache on a per-item basis.

For example, the below

import asyncio
from aiomemoize import memoize

async def main():
    memoized, invalidate = memoize(coro)
    results = await asyncio.gather(*[
        memoized('a'),
        memoized('a'),
        memoized('b'),
    ])
    print(results)

    invalidate('a')
    results = await asyncio.gather(*[
        memoized('a'),
        memoized('a'),
        memoized('b'),
    ])
    print(results)

    await memoized('a')

async def coro(value):
    print('Inside coro', value)
    await asyncio.sleep(1)
    return value

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

will output

Inside coro a
Inside coro b
['a', 'a', 'b']
Inside coro a
['a', 'a', 'b']

About

Memoize ayncio Python function calls, with manual invalidation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages