A decorator that coalesces concurrent function calls
Project description
Concurrent Coalesce
A Python decorator that coalesces concurrent function calls.
Description
This package provides a decorator that helps manage concurrent function calls by coalescing them, preventing redundant executions when multiple calls occur simultaneously.
Installation
pip install concurrent-coalesce
Features
- Prevents redundant concurrent execution of functions with identical arguments
- Works with both synchronous and threaded code
- Supports custom key functions for controlling how calls are grouped
- Compatible with Python 2.7+ and Python 3.x
- No external dependencies
Usage
Basic Usage
from concurrent_coalesce import coalesce
@coalesce()
def fetch_data(user_id):
print("Fetching data for user", user_id)
response = requests.get("https://api.example.com/users", params={"user_id": user_id})
response.raise_for_status()
return response.json()
When multiple threads call fetch_data() with the same user_id concurrently, only one thread will actually execute the function. All other threads will wait for the result and receive the same return value.
Custom Key Function
You can customize how arguments are grouped by providing a key function:
@coalesce(key_func=lambda user_id, **kwargs: user_id)
def fetch_user_data(user_id, include_history=False, include_preferences=True):
# This will coalesce based only on user_id, ignoring other parameters
response = requests.get("https://api.example.com/users", params={
"user_id": user_id,
"include_history": include_history,
"include_preferences": include_preferences
})
response.raise_for_status()
return response.json()
How It Works
The coalesce decorator:
- Intercepts function calls and generates a key based on the function arguments
- For the first call with a given key, the function executes normally
- Subsequent calls with the same key (before the first call completes) wait for the result
- All callers receive the same return value or exception
- After completion, the next call will trigger a new execution
sequenceDiagram
autonumber
participant T1 as Thread 1
participant T2 as Thread 2
participant C as Decorator
participant F as fetch_data()
T1 ->> F : Call to fetch_data()
activate C
activate F
T2 ->> C : Call to fetch_data()
Note right of C: Thread 2 is blocked while<br/>Thread 1 processes fetch_data()
F ->> T1 : Return from fetch_data()
deactivate F
C ->> T2 : Return from fetch_data()
deactivate C
API Reference
@coalesce(key_func=None, *args)
A decorator that coalesces concurrent calls to a function with the same arguments.
The decorator can be used in four ways:
- As a decorator
@coalesce() - As a decorator with key function:
@coalesce(key_func=my_key_func) - As a direct function call:
coalesce(my_function) - As a direct function call with key function:
coalesce(my_function, key_func=my_key_func)
Parameters:
key_func: Optional callable that takes*args, **kwargsand returns a hashable key. Defaults to using(args, frozenset(kwargs.items())).*args: If provided, must be a single callable that will be decorated. If no args are provided, returns the decorator function. If multiple args are provided, raises TypeError.
Raises:
TypeError: If key_func is not callable or if multiple arguments are provided in *args
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file concurrent_coalesce-0.1.0.tar.gz.
File metadata
- Download URL: concurrent_coalesce-0.1.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9123e56dbcab231675dd2e10478e2b146dde58f2cc79a0cda1596bd9988f14a1
|
|
| MD5 |
677ebf227385393d73fb771919f8efa2
|
|
| BLAKE2b-256 |
d00c8c5efb2a145b7cc7edd1c159b6cacbc04b2694e56842e36f11112471b0c3
|
Provenance
The following attestation bundles were made for concurrent_coalesce-0.1.0.tar.gz:
Publisher:
pypi.yml on claytonsingh/concurrent-coalesce-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
concurrent_coalesce-0.1.0.tar.gz -
Subject digest:
9123e56dbcab231675dd2e10478e2b146dde58f2cc79a0cda1596bd9988f14a1 - Sigstore transparency entry: 208152852
- Sigstore integration time:
-
Permalink:
claytonsingh/concurrent-coalesce-python@a301f7a32faa2019ec08884538b58d244ff0a1c5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/claytonsingh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@a301f7a32faa2019ec08884538b58d244ff0a1c5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file concurrent_coalesce-0.1.0-py3-none-any.whl.
File metadata
- Download URL: concurrent_coalesce-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d564ebf6dc1baaf6d7d1359e9dc1e59cd03dd419128b7b22a9ee90989b7c21e2
|
|
| MD5 |
80cafb7647697de62dd3ce47199825ee
|
|
| BLAKE2b-256 |
618329494a47e6c429d20063556bdb217139133aa649f75338db75d675a3678a
|
Provenance
The following attestation bundles were made for concurrent_coalesce-0.1.0-py3-none-any.whl:
Publisher:
pypi.yml on claytonsingh/concurrent-coalesce-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
concurrent_coalesce-0.1.0-py3-none-any.whl -
Subject digest:
d564ebf6dc1baaf6d7d1359e9dc1e59cd03dd419128b7b22a9ee90989b7c21e2 - Sigstore transparency entry: 208152855
- Sigstore integration time:
-
Permalink:
claytonsingh/concurrent-coalesce-python@a301f7a32faa2019ec08884538b58d244ff0a1c5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/claytonsingh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@a301f7a32faa2019ec08884538b58d244ff0a1c5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file concurrent_coalesce-0.1.0-py2-none-any.whl.
File metadata
- Download URL: concurrent_coalesce-0.1.0-py2-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b257ed9d7656b11b9ddd2942098f59e126a14b3930a111c174ea84b42de0cb8d
|
|
| MD5 |
171024e9ae3c2b35c481b9caf6b0dec0
|
|
| BLAKE2b-256 |
563c0aeeda2100844b92c0e5fbde30a5c945e82a8e6c6c614c97b9e003cc1fb6
|
Provenance
The following attestation bundles were made for concurrent_coalesce-0.1.0-py2-none-any.whl:
Publisher:
pypi.yml on claytonsingh/concurrent-coalesce-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
concurrent_coalesce-0.1.0-py2-none-any.whl -
Subject digest:
b257ed9d7656b11b9ddd2942098f59e126a14b3930a111c174ea84b42de0cb8d - Sigstore transparency entry: 208152857
- Sigstore integration time:
-
Permalink:
claytonsingh/concurrent-coalesce-python@a301f7a32faa2019ec08884538b58d244ff0a1c5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/claytonsingh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@a301f7a32faa2019ec08884538b58d244ff0a1c5 -
Trigger Event:
release
-
Statement type: