Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
B

BatchPool

> 编程语言
Open source

The one-stop generic task batching and management library

181 stars0 likes3 views
WebsiteGitHub

About

The one-stop generic task batching and management library

BatchPool

The one-stop generic task batching and management library.
Contributions are welcome to add features, flexibility, performance test coverage...

Features

  • Very low overhead, flexible, high test coverage, generic...
  • Tasks are executed in the order in which they are received
  • Supports Task, Func and Action
  • BatchPoolContainer states: Enabled / Paused
  • Dynamic batch size: update the size of the BatchPool
  • Callbacks: supports Task, Func and Action
  • Check the state of a task
  • Task Cancellation
  • batchPoolContainer Cancellation
  • Adding tasks in batch
  • Dynamic ordering
  • Dynamic custom ordering
  • Updatable dynamic ordering
  • Updatable dynamic custom ordering
  • Factory
  • Waiting for tasks to finish
  • BatchPoolContainerManager
  • BatchPoolContainerManager with DI

Supports Task, Func and Action

…

BatchPoolContainer states: Enabled / Paused

csharp
// Set isEnabled to configure the state of the batchPoolContainer at initialization
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: false);

Task aTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask);
await task.WaitForTaskAsync();

// Resume and forget
batchPoolContainer.ResumeAndForget();
// Or resume and wait for all task to finish
await batchPoolContainer.ResumeAndWaitForAllAsync();

// Then pause again to prevent new pending tasks to run
batchPoolContainer.Pause();

Dynamic batch size: update the size of the BatchPool

csharp
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: false);

// Increase or reduce the capacity and wait for it to finish updating. (The batchPoolContainer will need to wait if a reduction is requested while it is currently processing)
await batchPoolContainer.UpdateCapacityAsync(10);
// Perform the same operation in the background
batchPoolContainer.UpdateCapacityAndForget(10);

Callbacks: supports Task, Func and Action

csharp
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);

Task aTask = new Task(() => Console.WriteLine("Hello"));
// The callback will run as soon as the main task completes
Task aCallbackTask = new Task(() => Console.WriteLine("Hello"));

BatchPoolTask task = batchPoolContainer.Add(aTask, aCallbackTask);
await task.WaitForTaskAsync();

Check the state of a task

csharp
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);

Task aTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask);

bool isCanceled = task.IsCanceled;
bool isCompleted = task.IsCompleted;

Task Cancellation

csharp
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);

Task aTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask);

// Attempt to cancel
bool didCancel = task.Cancel();
// Attempt to cancel all pending tasks (pending = tasks that have not yet started processing due to the batch size, or the paused state of the BatchPool)
batchPoolContainer.RemoveAndCancelPendingTasks();

batchPoolContainer Cancellation

csharp
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true, cancellationToken: cancellationTokenSource.Token);
// All pending tasks will be Canceled
cancellationTokenSource.Cancel();

Adding tasks in batch

csharp
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);

Task aTask1 = new Task(() => Console.WriteLine("Hello"));
Task aTask2 = new Task(() => Console.WriteLine("Hello"));
List listOfTasks = new List() { aTask1, aTask2 };

ICollection tasks = batchPoolContainer.Add(listOfTasks);

Dynamic ordering

int order example:

…

string order example:

csharp
BatchPoolDynamicContainer batchPool = new BatchPoolDynamicContainer(batchSize, isEnabled: false);

var task1 = new Task(() => Console.WriteLine("Hello #1"));
var batchTask1 = batchPool.Add(task1, priority: "a");

var task2 = new Task(() => Console.WriteLine("Hello #2"));
var batchTask2 = batchPool.Add(task2, "b");

var task3 = new Task(() => Console.WriteLine("Hello #3"));
var batchTask3 = batchPool.Add(task3, "c");

await batchPool.ResumeAndWaitForAllAsync();

enum order example:

…

Dynamic custom ordering

Custom ordering simply by passing a new IComparer:

csharp
// Reverse the default order
public class StringReverseComparer : IComparer
{
    public int Compare(string? x, string? y)
    {
        return y!.CompareTo(x!);
    }
}

BatchPoolDynamicContainer batchPool = new BatchPoolDynamicContainer(batchSize, new StringReverseComparer(), isEnabled: false);

Updatable dynamic ordering

…

Updatable dynamic custom ordering

Custom ordering simply by passing a new IComparer:

csharp
// Reverse the default order
public class StringReverseComparer : IComparer
{
    public int Compare(string? x, string? y)
    {
        return y!.CompareTo(x!);
    }
}

BatchPoolUpdatableDynamicContainer batchPool = new BatchPoolUpdatableDynamicContainer(batchSize, new StringReverseComparer(), isEnabled: false);

Technical Note: More checks are required for BatchPoolUpdatableDynamicContainer over the BatchPoolDynamicContainer. However, the performance impact should not be noticeable as they occur at O(1). UpdatePriority does not modify the existing data structure, but instead allows the container to check if a priority is current and valid or a new has been added.

Factory

The BatchPoolFactory simply provides a quick way to instantiate a new BatchPoolContainer:

csharp
// Default queue
BatchPoolContainer batchPool = BatchPoolFactory.GetQueueBatchPool(batchSize, isEnabled: false);

// Dynamic ordering
BatchPoolDynamicContainer batchPool = BatchPoolFactory.GetDynamicallyOrderedBatchPool(batchSize, isEnabled: false);

// Dynamic ordering and update existing tasks priority
BatchPoolUpdatableDynamicContainer batchPool = BatchPoolFactory.GetUpdatableDynamicallyOrderedBatchPool(batchSize, isEnabled: false);

Waiting for tasks to finish

…

BatchPoolContainerManager

…

BatchPoolContainerManager with DI

csharp
IHost hostBuilder = Host.CreateDefaultBuilder()
    .ConfigureServices((_, services) => services.AddSingleton>())
    .Build();

using IServiceScope serviceScope = hostBuilder.Services.CreateScope();
IServiceProvider serviceProvider = serviceScope.ServiceProvider;

BatchPoolContainerManager batchPoolContainerManager = serviceProvider.GetRequiredService>();

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#asyncbatchbatch-processingc-sharp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言