Run multiple async functions with limited concurrency
Project description
plimit
Run multiple async functions with limited concurrency
Python port of the popular Node.js p-limit package. Uses asyncio to run async (and sync) functions with a configurable concurrency limit.
Install
pip install plimit
or with uv:
uv add plimit
Usage
import asyncio
from plimit import p_limit
async def main():
limit = p_limit(2) # max 2 concurrent tasks
async def fetch(url):
# ... your async work
return url
results = await asyncio.gather(
limit(fetch, "https://example.com/1"),
limit(fetch, "https://example.com/2"),
limit(fetch, "https://example.com/3"),
)
print(results)
asyncio.run(main())
API
p_limit(concurrency, *, reject_on_clear=False)
Returns a Limiter instance.
concurrency(int) — Maximum number of async functions running at the same time. Minimum:1.reject_on_clear(bool) — WhenTrue,clear_queue()rejects pending futures withClearQueueErrorinstead of silently discarding them. Default:False.
limit(fn, *args) → awaitable
Schedule fn(*args) to run under the concurrency limit. Returns an awaitable that resolves to the return value of fn.
fncan be an async function or a regular function.- Extra positional arguments are forwarded to
fn.
result = await limit(my_async_fn, arg1, arg2)
limit.active_count → int
Number of functions currently executing.
limit.pending_count → int
Number of functions waiting in the queue.
limit.concurrency → int (get/set)
Get or set the concurrency limit at runtime. When increased, queued tasks start immediately up to the new limit.
limit.concurrency = 10 # increase concurrency dynamically
limit.clear_queue()
Discard pending functions that have not started yet.
- If
reject_on_clear=True, pending futures are rejected withClearQueueError. - Does not cancel functions that are already running.
limit.map(iterable, fn) → awaitable list
Process an iterable through fn with limited concurrency. The mapper receives (item, index).
results = await limit.map([1, 2, 3], async_transform)
limit_function(fn, concurrency, *, reject_on_clear=False)
Convenience wrapper — returns a new async function that calls fn with limited concurrency.
from plimit import limit_function
limited_fetch = limit_function(fetch, concurrency=3)
result = await limited_fetch(url)
ClearQueueError
Exception raised for pending tasks when clear_queue() is called with reject_on_clear=True.
Comparison with JavaScript p-limit
JavaScript (p-limit) |
Python (plimit) |
|---|---|
const limit = pLimit(5) |
limit = p_limit(5) |
await limit(fn, ...args) |
await limit(fn, *args) |
limit.activeCount |
limit.active_count |
limit.pendingCount |
limit.pending_count |
limit.concurrency = 10 |
limit.concurrency = 10 |
limit.clearQueue() |
limit.clear_queue() |
limit.map(iter, fn) |
await limit.map(iter, fn) |
limitFunction(fn, opts) |
limit_function(fn, concurrency=N) |
AbortError on clear |
ClearQueueError on clear |
Requirements
Python 3.10+
License
MIT
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
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 plimit-1.0.0.tar.gz.
File metadata
- Download URL: plimit-1.0.0.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
975b83285f397b703634978ef7f9a3a4de994ed3640ce9d364243b91b5120310
|
|
| MD5 |
7fc026945f8222f2f35f8ad5ae966fc5
|
|
| BLAKE2b-256 |
619fbddc565f34d2f643bfd71fcac5bd491469145c1c8d8435adb3de1e51e30b
|
File details
Details for the file plimit-1.0.0-py3-none-any.whl.
File metadata
- Download URL: plimit-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb854f1d601ddb3bedff0fd537998f8644a17bc64bc34cc0d927efbe3a4999f0
|
|
| MD5 |
b57b3dde02c8f1febb706a49bf0e32fe
|
|
| BLAKE2b-256 |
84b1a25c1a75554fcb91e9063c063957731ab9ebb86ea3957ed0ae1b4ecd52de
|