Codibre.ConcurrentTaskRunner
0.0.1
See the version list below for details.
dotnet add package Codibre.ConcurrentTaskRunner --version 0.0.1
NuGet\Install-Package Codibre.ConcurrentTaskRunner -Version 0.0.1
<PackageReference Include="Codibre.ConcurrentTaskRunner" Version="0.0.1" />
paket add Codibre.ConcurrentTaskRunner --version 0.0.1
#r "nuget: Codibre.ConcurrentTaskRunner, 0.0.1"
// Install Codibre.ConcurrentTaskRunner as a Cake Addin #addin nuget:?package=Codibre.ConcurrentTaskRunner&version=0.0.1 // Install Codibre.ConcurrentTaskRunner as a Cake Tool #tool nuget:?package=Codibre.ConcurrentTaskRunner&version=0.0.1
Codibre.ConcurrentTaskRunner
A lib for concurrent, semaphore limited task runner under the hood
How to use?
Create an instance of options specifying the desired concurrency limit
ConcurrentTaskRunnerOptions options = new()
{
Limit = 3
};
Then, crate your runner instance
ConcurrentTaskRunner runner = new(options);
Finally, delegate the calls you want to limit concurrency
await runner.Run(MyCall);
Notice that, if the method Run never waits for MyCall to finish, it actually waits for a run slot to be freed, that is, if the concurrency limit hasn't been reached yet, it'll not wait and just pass through to MyCall execution in background.
Error handling
By default, any error the callback passed to Run throws is ignored, but you can treat it somehow informing a callback in the options to deal with it, like this:
ConcurrentTaskRunnerOptions options = new()
{
Limit = 3,
OnError = (Exception err) => Console.WriteLine(err.Message)
}
Errors thrown by OnError callback are not ignored, though, so write it carefully making sure it'll not throw anything so you don't get an untreated exception.
Procressing an Enumerable or AsyncEnumerable
It's also possible to use ConcurrentTaskRunner to process in a concurrently limited pace an enumerable or async enumerable instance. You just need to use our extension method RunConcurrently, like this:
await enumerable
.RunConcurrently(runner, (x) => MyCall(x));
Notice that a AsyncEnumerable may be infinite, so this is a blocking line in your code if that's the case. A CancellationToken instance can be used to create a stoppage condition:
await enumerable
.RunConcurrently(runner, (x) => MyCall(x), cancellationToken);
In this implementation, cancellationToken will not throw an error, but before executing the callback, it'll be checked to validate whether the cancellation had been requested or not, and it's not propagated to the callback. If you need it, though, you can propagate it yourself, like this:
await enumerable
.RunConcurrently(runner, (x) => MyCall(x, cancellationToken), cancellationToken);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
-
net7.0
- System.Linq.Async (>= 6.0.1)
-
net8.0
- System.Linq.Async (>= 6.0.1)
-
net9.0
- System.Linq.Async (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.