Parallel.ForEachAsync with BlockingCollection #58680
-
What is the best practice for the new I'm looking to update my implementation based on @stephentoub blog https://devblogs.microsoft.com/pfxteam/implementing-a-simple-foreachasync-part-2/ with the new net6 implementation but not sure whether I can pass the BlockingCollection as the source or should still use the GetConsumingEnumerable() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Just passing the collection will enumerate but not consume the contents as it's doing so.
You'd be better off using a channel rather than having threads block with a "blocking" collection waiting for the next item to arrive. The primary purpose of ForEachAsync is to enable iteration without blocking. |
Beta Was this translation helpful? Give feedback.
-
Stephen thanks for the reply. My initial ask was ill formulated. In any case I'll be looking into leveraging Channels. To clarify --
In the current codebase, we followed your advice and went with the
|
Beta Was this translation helpful? Give feedback.
Stephen thanks for the reply. My initial ask was ill formulated. In any case I'll be looking into leveraging Channels.
To clarify --
GetConsumingEnumerable()
is indeed the functionality that we need, but you warn of performance hits in this post: