I was implementing a Producer-Consumer using BufferBlock
. The code is working well.
static async Task Produce(ITargetBlock<int> queue)
{
try
{
// Post messages to the block asynchronously.
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Sending: {0}", i);
await queue.SendAsync(i);
}
}
finally
{
queue.Complete();
}
}
static async Task Consume(ISourceBlock<int> queue)
{
// Read messages from the block asynchronously.
while (await queue.OutputAvailableAsync())
{
int value = await queue.ReceiveAsync();
Console.WriteLine("Receiving: {0}", value);
}
}
static void Main(string[] args)
{
// Create a BufferBlock<int> object.
var queue = new BufferBlock<int>();
try
{
var produce = Produce(queue);
var consume = Consume(queue);
Task.WaitAll(produce, consume, queue.Completion);
}
catch (Exception exception)
{
Console.WriteLine("An exception was thrown: {0}", exception.Message);
Console.WriteLine("Terminating...");
}
}
Now I have a throttling issue, which is I want to the maximum concurrent number for consumer is 4. I want to use SemaphoreSlim
bot not sure how to apply it.
Notice: it is a concurrent scheduler question, not parallelism problem.
ActionBlock
with a limited concurrency of 4?ActionBlock
. I thought it is for parallelism instead of concurrentcy. Parallelism is for multiple cpu cores.