Deduplicate concurrent function calls.
Project description
dedup_me
dedup_me is a simple library for concurrent in-flight deduplication of functions. This can be useful for e.g. API calls or DB access. Instead of querying the same data multiple times, this library allows you to query once and share the result.
Note: This library does not cache results. After the result is returned, new consumers will call the function(API, DB, ...) again.
Installation
pip install dedup-me
Usage
AsyncIO
import asyncio
import random
from dedup_me import async_dedup
# @async_dedup('static-key') # if the arguments don't matter, you can use a static key
@async_dedup(key = lambda x: f'dynamic-key-{x}') # or pass in a key function that accepts all arguments
async def expensive_function(x: int) -> int:
print('expensive function called')
await asyncio.sleep(x)
return random.randint(0, 10)
async def main() -> None:
# call the function 10 times
# this should only print 'expensive function called' once
results = await asyncio.gather(
*(
expensive_function(1)
for _ in range(10)
)
)
print(results)
# all results should be the same
results_list = list(results)
assert results_list == [results_list[0]] * 10
Alternatively, without the decorator:
import asyncio
import random
from dedup_me import AsyncDedup
async def expensive_function() -> int:
print('expensive function called')
await asyncio.sleep(1)
return random.randint(0, 10)
async def main() -> None:
dedup = AsyncDedup()
await asyncio.gather(
*(
# functions are grouped by the key, choose something that represents the function and its arguments
# the second argument must be a function without arguments that returns an awaitable
dedup.run('some-key', lambda: expensive_function())
for _ in range(10)
)
)
Threads
For threading just use the threading_dedup
decorator or ThreadingDedup
.
from time import sleep
from dedup_me import threading_dedup, ThreadingDedup
@threading_dedup('key')
def expensive_function() -> None:
sleep(1)
expensive_function()
# or
dedup = ThreadingDedup()
dedup.run('key', lambda: sleep(1))
Forcing a new execution
You can enforce a new execution by passing force_new = True
to the run
function.
When using the decorator, you can add a callable that will receive all arguments and returns a boolean.
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 dedup_me-0.2.0.tar.gz
.
File metadata
- Download URL: dedup_me-0.2.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.10.9 Linux/5.15.86-1-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5233b4e8c5990b3eac771145feeb355f728b865d0276e77b58afe139f52f42e9 |
|
MD5 | 8c726c4651a85a2cf46931fa30046850 |
|
BLAKE2b-256 | b69a01e727f08e3cfdecf6b390c5d9a5b87dc4bb6f1a7a2ee00c17cfb47602ac |
File details
Details for the file dedup_me-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: dedup_me-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.10.9 Linux/5.15.86-1-lts
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4181e986036599dce443b38d2b45253816cb35dd91cb0a595609f4d3c425c4f7 |
|
MD5 | 32b3e7572fb9f11ae1e3e9eff06b00c6 |
|
BLAKE2b-256 | bcb5498fde6dcc211f6057332cb9c760fcef9447f8a2f9cc48fd8452a988572c |