Asynchronous batch processing (queue) in C#
Hey guys, I have some batches to process and to do so I have to send each of my batches to external APi to process. I have no experience with async programming so I simplified it a bit by using Parallel.Foreach that create thread/task for each API calling and waiting for response. The reason I dont want to call API synchronously Is that processing of one batch can take a one sec or 30min (max). With the Parallel.Foreach solution it looks it works fine but form what I read on internet it's not a good approach so I want to implement kind of async queue that will take a few items from my collection of batches starts processing and after some of them is finished replace it with another etc. I tried to came up with something using ChatGPT but it only process specified number of batches (3 in my case) and than it does nothing but even if the code is short there are lot's of new concepts for me so I have no idea how to fixed this.
13 Replies
I can't reproduce your described error. Here is the output of my experiment using your method:
Hm, that's weird. I have list of these ids and only first 3 of them I can see in logs ...
all my code. As you can see this runs in a console app, and Im guessing yours is an asp.net backend?
yeah, exactly
.NET 4.7.2
oh, I'm running on .NET 9
4.7 is over 8 years old at this point
as in, 4.7 got superceded 8 years ago.
But I'm sure it's because of .NET version. Yeah, it is and we are are working on migration to newer framework but ... 🙂 But Im going to try it in separate console app too. It's a shame but I didn'd do it
*I'm not sure it's because of .NET version
Yeah, try the standalone console app first
Hm in standalone app it works for me too (.NET 5). It's weird
make sure you try with 4.7.2
.net 5 is on .net core, its not remotely the same as 4.7.2
I tried it also for 4.7.2 but it works the same. Guess it somehow related to webapp. Maybe one more question. Is that Parallel foreach really that bad approach for my case ?
you'd want
Parallell.ForEachAsync
at the very least
since the thing you are doing in each iteration is waiting for IO, which is async
that said, you dont want to fire off thousands of http requests at the same time,I definitely dont want to fire thousands HTTP requests but I can limit them to 10 or something like that. These batches are about 2000 (these are daily batches so one for every day from about 2020, something like that).
as long as you still have some kind of semaphore or other limiting solution, I dont see why parallel.foreachasync wouldnt work fine