✅ how can 1000 requests be completed quickly?
I have a list of ids
for each id I would go to database to find smth
and let's say I'd like to work with each id as fast as possible
is it correct code? how it will work with 1000 items in array?
will 1000 threads be created?
20 Replies
you probably want Parallel For
hm
let's see
ivefifthsence#7281
REPL Result: Success
Compile: 617.175ms | Execution: 108.376ms | React with ❌ to remove this embed.
108.376ms
ivefifthsence#7281
REPL Result: Success
Compile: 612.330ms | Execution: 87.397ms | React with ❌ to remove this embed.
do not use this to measure it lmfao
87.397ms! heh
these are incomparable
also not useful at all for measurement
incomparable to your real usecase i mean
this also literally doesn't execute anything
as you can see by the lack of any console output
Hmm, a database call is io bound, so definitely no parallel.for or task.run. Just use normal async await
Something like this but with a database call instead of task.delay:
Also,
Console.WriteLine
is thread-safe (it's locking the stream).
So every thread is waiting for each-other when another thread is done writing the line.Also these didn't even execute the code you wrote
kocha's approach is correct
one of two correct possibilities
You can also consider Parallel.ForEachAsync if you don't want to flood your db with queries
the one where you START all of the requests synchronously, at roughly the same time, and wait for them all to complete asynchronously
I recommend this article I found some time ago, specifically the "parallel, but smarter" example. it's pretty much what kocha said above, but also consider batching: https://www.michalbialecki.com/en/2018/04/19/how-to-send-many-requests-in-parallel-in-asp-net-core/
admin
Michał Białecki Blog
How to send many requests in parallel in ASP.Net Core - Michał Biał...
I want to make 1000 requests! How can I make it really fast? Let’s have a look at 4 approaches and compare their speed. Preparations In order to test different methods of handling requests, I created a very simple ASP.Net Core API, that return user by his id. It fetches them from plain old MSSQL… Continue reading How to send many requests in par...
To check whether Parallel really speeds up calculations, I wrote this code to search for all primes less than 100,000 in a "synchronous" and "parallel" style. Collected the found numbers in ConcurrentBag.
parallel is faster
that doesnt mean it will be for the data base access parallel will be better too
for the db stuff the bottleneck is the IO between ur application and the db, not the compution power.
with parallel u would be doing blocking IO, meaning the thread will be blocked/waiting until it has data to process (basically doing nothing in the mean time)
in an async approach it would not be blocking, the thread would do other stuff in the mean time, which would be faster again
yes, parallel is faster for CPU bound work, because it leverages more CPU cores
database I/O is I/O-bound work
You have good choices, first one is using
Parallelism
and the second one is using Caching
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.