C
C#2y ago
Mastalt

❔ Help with MultiThreading

Image 1 works, but is really slow Image 2 doesn't work (No output for the WriteLine)
19 Replies
mtreit
mtreit2y ago
Why are you using async? Use $code to paste code, chopped off screenshots make it hard to say what your code is actually doing.
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
mtreit
mtreit2y ago
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.
Mastalt
Mastalt2y ago
does that rly help with my situation here?
mtreit
mtreit2y ago
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.
Mastalt
Mastalt2y ago
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
mtreit2y ago
How many tasks are you spinning up?
Mastalt
Mastalt2y ago
idk tbh, 1 for every hours from 2020-05-01 to 2020-09-30 I forgot a comma in my 12k apparently 1.2k around that
mtreit
mtreit2y ago
Yeah, you're completely exhausting the thread pool...which is really bad.
Mastalt
Mastalt2y ago
soooo, any way around that?
mtreit
mtreit2y ago
You need to use async / await.
Mastalt
Mastalt2y ago
will try, did not actually think that was the issue, but ill try
mtreit
mtreit2y ago
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.
Mastalt
Mastalt2y ago
gotcha
mtreit
mtreit2y ago
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. It will take forever to get to 1000 running. I have a demo of exactly this behavior in the talk I gave at the Solution1 conference a week or so back. (https://youtu.be/8lUs9ukVrFY) 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.
Mastalt
Mastalt2y ago
Wow, it actually worked, thank you very much, also i'll give your vid a watch, still trying to learn more 😅
mtreit
mtreit2y ago
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.
Mastalt
Mastalt2y ago
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
Accord
Accord2y ago
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.