C#
C#

help

Root Question Message

TheMasterPvp79
TheMasterPvp7912/1/2022
❔ Help with MultiThreading

Image 1 works, but is really slow
Image 2 doesn't work (No output for the WriteLine)
mtreit
mtreit12/1/2022
Why are you using async?
mtreit
mtreit12/1/2022
Use $code to paste code, chopped off screenshots make it hard to say what your code is actually doing.
mtreit
mtreit12/1/2022
And you shouldn't be calling async methods and then calling .Wait() or .Result on them. You should be awaiting them if you really need things to be async.
TheMasterPvp79
TheMasterPvp7912/1/2022
does that rly help with my situation here?
mtreit
mtreit12/1/2022
It's unclear to me what the issue is from what you posted, but the code is chopped off so it's hard to see exactly what you're doing.
TheMasterPvp79
TheMasterPvp7912/1/2022
The problem is that if i single thread, it works, but i dont think i want to do a request to nasa(which take ~5 sec) for 12k+ hours, so i multithread, and it doesn't want to work for some reason, just is a standstill
mtreit
mtreit12/1/2022
How many tasks are you spinning up?
TheMasterPvp79
TheMasterPvp7912/1/2022
idk tbh, 1 for every hours from 2020-05-01 to 2020-09-30
TheMasterPvp79
TheMasterPvp7912/1/2022
I forgot a comma in my 12k apparently
TheMasterPvp79
TheMasterPvp7912/1/2022
1.2k
TheMasterPvp79
TheMasterPvp7912/1/2022
around that
mtreit
mtreit12/1/2022
Yeah, you're completely exhausting the thread pool...which is really bad.
TheMasterPvp79
TheMasterPvp7912/1/2022
soooo, any way around that?
mtreit
mtreit12/1/2022
You need to use async / await.
TheMasterPvp79
TheMasterPvp7912/1/2022
will try, did not actually think that was the issue, but ill try
mtreit
mtreit12/1/2022
Which means while the call is in progress it won't consume any thread pool threads and then those threads can be used to start other tasks.
TheMasterPvp79
TheMasterPvp7912/1/2022
gotcha
mtreit
mtreit12/1/2022
By default the thread pool will have Environment.ProcessorCount number of threads available. Let's say you have eight cores, that means it has 8 threads in the thread pool. Every time you start a task, if al thread pool threads are currently in use it will wait half a second before creating a new one.
mtreit
mtreit12/1/2022
It will take forever to get to 1000 running.
mtreit
mtreit12/1/2022
I have a demo of exactly this behavior in the talk I gave at the Solution1 conference a week or so back.
mtreit
mtreit12/1/2022
You probably don't want to actually spin up 1,000 calls all at once - you will probably get throttled by the service you are calling. You might want to make the async calls in batches and await Task.WhenAll(tasks) on the batch, then repeat.
TheMasterPvp79
TheMasterPvp7912/1/2022
Wow, it actually worked, thank you very much, also i'll give your vid a watch, still trying to learn more 😅
mtreit
mtreit12/1/2022
Actually whatever service you are calling should really have some kind of batch API so you don't have to make so many chatty calls.
TheMasterPvp79
TheMasterPvp7912/1/2022
I'm using NasaApi, which take a start and end day, theoratically, i could put the start and end, foreach that and take all the data, however, my longitude and latitude changes every 6 seconds, but that would take too long so i check it for the lon/lat at every hour
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy