Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IAsyncEnumerable<T> for AsyncProducerConsumerQueue<T> #226

Open
bradmarder opened this issue Nov 21, 2020 · 4 comments
Open

IAsyncEnumerable<T> for AsyncProducerConsumerQueue<T> #226

bradmarder opened this issue Nov 21, 2020 · 4 comments
Assignees

Comments

@bradmarder
Copy link

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.

@StephenCleary StephenCleary self-assigned this Nov 25, 2020
@StephenCleary
Copy link
Owner

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.

@markusschaber
Copy link

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?

@StephenCleary
Copy link
Owner

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.

@bradmarder
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants