Skip to main content

Memoize asyncio Python function calls

Project description

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']

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for aiomemoize, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size aiomemoize-0.0.3-py3-none-any.whl (3.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size aiomemoize-0.0.3.tar.gz (2.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page