0

I have a foreach loop that sends data to a GRPC API. I want my loop to send multiple requests at the same time but limit the number of requests to e.g. 10. My current code is the following:

foreach (var element in elements) { var x = new Thread(() => SendOverGrpc(element)); x.Start(); } 

But with that code, the software "immediately" sends all requests. How can I limit the number of requests to e.g. 10? As soon as one of my 10 requests is finished, I want to send the next one.

2
  • 1
    You don't need to use low-level threading to achieve concurrent requests in network IO (the main reason being it's incredibly wasteful: doing new Thread consumes a big chunk of RAM for the stack and thread-local storage, which is why .NET comes with the ThreadPool). Instead you should use proper async sockets which enables programs to achieve concurrent request processing very efficiently. Commented Feb 15, 2023 at 22:28
  • 1
    Why are you using threads and not tasks? Commented Feb 16, 2023 at 7:22

1 Answer 1

2

Easiest way is Parallel.Foreach, eg

Parallel.ForEach(elements, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, element => { SendOverGrpc(element); }); 
Sign up to request clarification or add additional context in comments.

8 Comments

This is bad advice though: threads should not be used as the primary means of multiplexing network IO, even when used via Parallel.ForEach. Async IO does not require multiple threads and using that instead of Thread-based approaches avoids numerous bugs relating to cross-thread synchronization and race-conditions.
Using 10 threads from the thread pool is trivial, as long as there aren't a many concurrent invocations of this method.
@Dai can you please elaborate regarding "Async IO", are you sure you aren't confusing parallel versus asynchronous?
@Dai For grpc it might indeed be a bad advice, however according to the title this would be a valid answer. Perhaps you could post a link about async IO+Grpc. I agree that it's something that needs to be taken into account
@OlegI True parallelism in network IO just isn't a thing (unless you have a multihomed network adapter, I suppose). While asynchronous, non-blocking, sockets make it possible to effectively fire-off many network requests/messages/packets at once using only a single application/userland program thread and let the OS's network stack handle any concurrency for you.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.