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
Metadata
Release files for plimit 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| plimit-1.0.0.tar.gz | 15.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| plimit-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.9 kB
Release files / plimit-1.0.0.tar.gz
| Download URL | plimit-1.0.0.tar.gz |
|---|---|
| Size | 15.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
975b83285f397b703634978ef7f9a3a4de994ed3640ce9d364243b91b5120310
|
|
BLAKE2b-256 checksum How to use checksums |
619fbddc565f34d2f643bfd71fcac5bd491469145c1c8d8435adb3de1e51e30b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / plimit-1.0.0-py3-none-any.whl
| Download URL | plimit-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cb854f1d601ddb3bedff0fd537998f8644a17bc64bc34cc0d927efbe3a4999f0
|
|
BLAKE2b-256 checksum How to use checksums |
84b1a25c1a75554fcb91e9063c063957731ab9ebb86ea3957ed0ae1b4ecd52de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|