https://github.com/guildofcalamity/concurrentutils
Concurrent utility library for Task
https://github.com/guildofcalamity/concurrentutils
Last synced: 11 months ago
JSON representation
Concurrent utility library for Task
- Host: GitHub
- URL: https://github.com/guildofcalamity/concurrentutils
- Owner: GuildOfCalamity
- License: mit
- Created: 2024-09-11T20:57:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-12T21:19:18.000Z (over 1 year ago)
- Last Synced: 2025-01-11T05:28:10.682Z (about 1 year ago)
- Language: C#
- Size: 312 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# C# Concurrent Utilities
---
### I have ported this entire project and made it usable from the old `xproj` format. I have made this a multi-framework project (.NET 4.8.1 & .NET 6.0) and also applied fixes and improvements where needed.
### The original can be found [here](https://github.com/jorgebay/concurrent-utils).
---
Provides classes and methods useful in concurrent programming.
## Functionalities
### `Times(long times, int limit, Func method)`
Executes an asynchronous method n number of times, limiting the amount of operations in parallel without blocking.
Returns a `Task` that is completed when all Tasks are completed or is faulted when any of the Tasks transition to
faulted state.
Suitable for benchmarking asynchronous methods with different maximum amount of parallel operations.
**Example**
```csharp
// Execute MyMethodAsync 1,000,000 times limiting the maximum amount of parallel async operations to 512
await ConcurrentUtils.Times(1000000, 512, (index) => MyMethodAsync());
```
### `Map(IList source, int limit, Func> method)`
Asynchronously projects each element of a sequence into a new form, limiting the amount of operations in parallel
without blocking.
Returns a `Task` that gets completed with the transformed elements or faulted when any of the transformation
operations transition to faulted state.
**Example**
```csharp
var urls = new []
{
"https://www.google.com/",
"https://www.microsoft.com/net/core",
"https://www.nuget.org/",
"https://dotnet.github.io/"
};
// Asynchronously get the http response of each url limiting
// the maximum amount of parallel http requests to 2
string[] responses = await ConcurrentUtils.Map(urls, 2, url => client.GetStringAsync(url));
```
### `CreateQueue(int limit, Func method)`
Creates collection of objects to which apply the asynchronous method in a first-in first-out manner. Items added to
the queue are processed in parallel according to the given limit.
Returns a `IJobQueue` instance that can be used to enqueue items.
**Example**
```csharp
// Create the queue providing the method that is going to be used to asynchronously process each item
// and the max amount of parallel operations (in this case 2)
IJobQueue jobQueue = ConcurrentUtils.CreateQueue(2, url => client.GetStringAsync(url));
// Add items to the queue that are going to be processed
Task t1 = jobQueue.Enqueue("https://www.google.com/");
Task t2 = jobQueue.Enqueue("https://www.microsoft.com/net/core");
Task t3 = jobQueue.Enqueue("https://www.nuget.org/");
Task t4 = jobQueue.Enqueue("https://dotnet.github.io/");
// Items are processed as FIFO queue, without exceeding the max amount of parallel operations limit
await Task.WhenAll(t1, t2, t3, t4);
```
---
