Memoize asyncio Python calls with a per-result TTL
Project description
aiomemoizettl
Memoize asyncio Python calls with a per-result TTL
Installation
pip install aiomemoizettl
Usage
For a coroutine whose arguments are hashable, you can create a memoized version by passing it to memoize_ttl
, along with a function that converts its return value to a TTL.
For example, the below
import asyncio
from aiomemoizettl import memoize_ttl
async def main():
memoized = memoize_ttl(coro, get_ttl=lambda result: result['ttl'])
results = await asyncio.gather(*[
memoized(1),
memoized(2),
])
await asyncio.sleep(1)
results = await asyncio.gather(*[
memoized(1),
memoized(2), # Will used the cached value of `coro(2)`
])
async def coro(value):
print('Inside coro', value)
return {'ttl': value, 'some-other': 'data'}
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
will output
Inside coro 1
Inside coro 2
Inside coro 1
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
aiomemoizettl-0.0.3.tar.gz
(2.1 kB
view hashes)
Built Distribution
Close
Hashes for aiomemoizettl-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07a6becac60f6cd2604b9f2b73bcd9a50079a0b7b55e2a4e45b1eec5a3ea9659 |
|
MD5 | 4da3da0cc6b28a5e27569758367f0b57 |
|
BLAKE2b-256 | 364959ad30138158b09ee058c72d053461c15dcc3f43fe26f53fed98aab3c32e |