Skip to main content

aiomemoizeconcurrent CircleCI Test Coverage

Memoize concurrent asyncio Python coroutine calls. This offers short-lived memoization: for any given set of arguments, the cache lasts only for the length of a single call.

Installation

pip install aiomemoizeconcurrent

Usage

For a coroutine whose arguments are hashable, you can create a memoized version by passing it to memoize_concurrent. Any concurrent calls to this version that have the same arguments will result in only a single run of original coroutine.

For example, creating 3 concurrent invocations of a coroutine where 2 of them have identical arguments

import asyncio
from aiomemoizeconcurrent import memoize_concurrent

async def main():
    memoized_coro = memoize_concurrent(coro)

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

    await memoized_coro('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 only run coro twice, as shown by the output

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

Use cases

This can be used to memoize a function making calls to an API, and especially if

  • you expect many concurrent calls;
  • identical concurrent calls are idempotent;
  • there are enough such calls that are identical to justify such a caching layer.

It can also be used to avoid concurrency edge cases/race conditions with multiple tasks accessing shared resources. For example, multiple tasks may need to dynamically create shared UDP sockets. To ensure that this dynamic generation isn't called by multiple tasks at the same time for the same address, it can be wrapped with memoize_concurrent.

The function memoize_concurrent works with both coroutines, and functions that return a future.

Metadata

Release files for aiomemoizeconcurrent 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiomemoizeconcurrent 0.0.8
File Size Uploaded
aiomemoizeconcurrent-0.0.8.tar.gz 2.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiomemoizeconcurrent 0.0.8
File Interpreter ABI Platform
aiomemoizeconcurrent-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 5.9 kB

Release files / aiomemoizeconcurrent-0.0.8.tar.gz

Download URL aiomemoizeconcurrent-0.0.8.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ecc64b22e5d4afad0e4ddc66575d08352c4ac4bdf177b89c7939c20440120d5b
BLAKE2b-256 checksum
How to use checksums
df5a0fa0e1c8bfd96d49787929f0d72e3f981239ef539c91c4fb733aa15f5cdd
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / aiomemoizeconcurrent-0.0.8-py3-none-any.whl

Download URL aiomemoizeconcurrent-0.0.8-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dfc05e9a95724dae9c95f612929905dcccebc9011da7343b01d33c74855e7a43
BLAKE2b-256 checksum
How to use checksums
92a9b4f81f3b6a963e2b5722776ff403a579a51e3dc7fad476e2d1a04e8d92b2
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page