AsyncEx icon indicating copy to clipboard operation
AsyncEx copied to clipboard

IAsyncEnumerable<T> for AsyncProducerConsumerQueue<T>

Open bradmarder opened this issue 5 years ago • 4 comments

I currently use this approach and it works fine

var items = new AsyncProducerConsumerQueue<int>();

while (await items.OutputAvailableAsync())
{
    var item = await items.DequeueAsync();
}

I was wondering if it's possible to have an IAsyncEnumerable<T> method on AsyncProducerConsumerQueue<T> so that I can do this instead.

await foreach (var item in items.DequeueItemsAsync()) { }

My only qualms with the the OutputAvailable/Dequeue approach is that in between the 2 methods, another thread could theoretically empty the queue. Anyway, thank you for this awesome library.

bradmarder avatar Nov 21 '20 04:11 bradmarder

Yes, certainly. It could use an update to support async enumerables.

In the meantime, you may want to check out Channels. They are the most modern implementation of an async producer/consumer queue.

StephenCleary avatar Nov 25 '20 00:11 StephenCleary

Provocative thoughts: Is there any functionality which AsyncProducerConsumerQueue<T> provides on top of what Channels provide? Should AsyncProducerConsumerQueue<T> be deprecated in favour of channels?

markusschaber avatar Nov 25 '20 10:11 markusschaber

Oh, that's not provocative at all. :)

For the last couple of years, I've been steering everyone I can towards Channels. I wouldn't say AsyncProducerConsumerQueue<T> is deprecated (it's still supported), but Channels is faster and has full support for modern techniques like async streams. I mostly use Channels myself these days.

StephenCleary avatar Nov 25 '20 13:11 StephenCleary

@StephenCleary thanks for the advice about Channels, it's exactly what I was looking for.

bradmarder avatar Nov 25 '20 16:11 bradmarder