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.

Source Distribution

aiomemoize-0.0.3.tar.gz (2.0 kB view details)

Uploaded Source

Built Distribution

aiomemoize-0.0.3-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file aiomemoize-0.0.3.tar.gz.

File metadata

  • Download URL: aiomemoize-0.0.3.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6

File hashes

Hashes for aiomemoize-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d938faaeb7569bdb23d22636d5a3b9c0f23e70eee31c0636f94ba892a5adab7f
MD5 0c724b9fabfe01e7c5e66dd5e4ae3dc6
BLAKE2b-256 bac063535cf9caa105841274e5aa93f1ad630786343e63adcde05d47a258c502

See more details on using hashes here.

File details

Details for the file aiomemoize-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: aiomemoize-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6

File hashes

Hashes for aiomemoize-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f43482f396765038af49b33d4a4915b24e403049fc777883ad25cb3e5ac38b5c
MD5 75b5cc8a0f65a779809f74790bbd64cc
BLAKE2b-256 a9f24de59365566e454aa880db8d2a6369349f8fa726d1d2c472a1309eb6ba70

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page