Memoize concurrent asyncio Python function calls
Project description
aiomemoizeconcurrent
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiomemoizeconcurrent-0.0.8.tar.gz
.
File metadata
- Download URL: aiomemoizeconcurrent-0.0.8.tar.gz
- Upload date:
- Size: 2.4 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecc64b22e5d4afad0e4ddc66575d08352c4ac4bdf177b89c7939c20440120d5b |
|
MD5 | cba76ece3a2fbb452ac8f28052747f24 |
|
BLAKE2b-256 | df5a0fa0e1c8bfd96d49787929f0d72e3f981239ef539c91c4fb733aa15f5cdd |
File details
Details for the file aiomemoizeconcurrent-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: aiomemoizeconcurrent-0.0.8-py3-none-any.whl
- Upload date:
- Size: 3.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfc05e9a95724dae9c95f612929905dcccebc9011da7343b01d33c74855e7a43 |
|
MD5 | 10cbddecceaf2d4290db77fe79891c19 |
|
BLAKE2b-256 | 92a9b4f81f3b6a963e2b5722776ff403a579a51e3dc7fad476e2d1a04e8d92b2 |