BufferBlock
을 사용하여 프로듀서 - 소비자를 구현하고있었습니다. 코드가 잘 작동하고 있습니다. async 동시 스케줄러가있는 Producer-Consumer 응용 프로그램
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...");
}
}
지금 나는 소비자에 대한 최대 동시 번호에 원하는 수있는 조절 문제는, 내가 그것을 적용하는 방법을 잘
SemaphoreSlim
봇을 사용하려면 4입니다 있습니다.
알림 : 병렬 스케줄러 문제가 아닌 동시 스케줄러 질문입니다.
당신은 "소비자에 대한 동시 번호"무엇을 의미합니까 여기에 처리하는 확장 방법입니까? – i3arnon
우리는 언제든지 4 개 이상의 아이템을 소비 할 수 없다고 말하십시오. –
@Love : 제한된 동시성이 4 인'ActionBlock'을 왜 사용하지 않으시겠습니까? –