❔ Help with MultiThreading
Image 1 works, but is really slow
Image 2 doesn't work (No output for the WriteLine)
19 Replies
Why are you using async?
Use $code to paste code, chopped off screenshots make it hard to say what your code is actually doing.
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/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.
does that rly help with my situation here?
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.
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
How many tasks are you spinning up?
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
Yeah, you're completely exhausting the thread pool...which is really bad.
soooo, any way around that?
You need to use async / await.
will try, did not actually think that was the issue, but ill try
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.
gotcha
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.Wow, it actually worked, thank you very much, also i'll give your vid a watch, still trying to learn more 😅
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.
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
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.