Run multiple future-returning & async functions with limited concurrency
This is a port of p-limit from javascript to dart.
flutter pub add p_limit
import 'package:p_limit/p_limit.dart';
void main() async {
final limit = PLimit<string>(1);
final input = [
limit(() => fetchSomething('foo')),
limit(() => fetchSomething('bar')),
limit(() => doSomething())
];
// Only one future is run at once
final results = await Future.wait(input);
print(results);
}
Returns a limit
function.
Type: number
Minimum: 1
Default: Infinity
Concurrency limit.
Returns the future returned by calling fn()
.
Type: Function
Future-returning/async function.
The number of futures that are currently running.
The number of futures that are waiting to run (i.e. their internal fn
was not called yet).
Discard pending futures that are waiting to run.
This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
Note: This does not cancel futures that are already running.
How is this different from the async_task
package?
This package is only about limiting the number of concurrent executions, while async_task
is a fully featured queue implementation similar to classic thread pools and with lots of different options.
You probably don't need this optimization unless you're pushing a lot of functions. If you are in need of this, make a pull request or an issue on Github.
- async_task - Asynchronous tasks and parallel executors
- concurrent_queue - Priority queue with concurrency control
If you have an issue, let me know on Github issues
Since this is a port of p-limit, I'd like to thank the creator for the amazing work and maintenance.