Memoize asyncio Python function calls
Project description
aiomemoize
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
. Any calls to this version that have the same arguments will result in only a single run of original coroutine.
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
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
aiomemoize-0.0.1.tar.gz
(2.0 kB
view hashes)
Built Distribution
Close
Hashes for aiomemoize-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eda03cfc411f771db0aedd66ef7cd87f8ec1f782ddb9b54d6fb09b77ebefc4e |
|
MD5 | ff017404d16bf5095faccc53cba3bfd0 |
|
BLAKE2b-256 | a69ed1e79e700147a6039aadeab088090f4030cd46678e89ea042c305517f6fa |