Call to async method waiting for execution even without await
I am trying to execute a async method multiple times in parallel. For this I am using the following code:
I added a sleep inside the method to verify parallel execution. But when I debug the code, the debug waits for the sleep time on the method call before moving ahead. What am I doing wrong here?
17 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
but shouldn't the sleep be executed in a separate thread?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Using async doesn't by itself add new threads
Async lets a thread pick up another bit of work when it hits an await, rather than sitting there blocked
oh, so sleep will block all the threads
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
There's only one thread here
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
ok, so my code wont execute the multiple Method calls in parallel? on changing Sleep to Thread.Delay, it is no longer waiting for the time, but it waits at the await Task.WhenAll
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
To execute the inner method in parallel you can use
Parellel.For
: https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-for-loop\How to: Write a Simple Parallel.For Loop
Learn to write Parallel.For loops in .NET in which you don't need to cancel the loop, break out of loop iterations, or maintain any thread-local state.
Correct. There's one thread (which is the one which called into the method originally), and it's keeping itself busy executing another bit of code every time it hits an await
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
async
methods do not yield the thread until the first await
is hit
And even then it may not yield if the awaited task can complete synchronously, i.e. you are trying to read from a network stream where the data is immediately available in a buffer
It only yields the thread to continue if it actually needs to wait on something
so, for example:
If the data is coming in fast enough such that ReadDataAsync
has the data requested available to process in a local buffer immediately, this method will run fully synchronously
The current thread is only yielded to continue processing if an await
is hit and that await
needs to actually wait on something
Since everything before the first await
can be processed synchronously, it always is.Are there any drawbacks of using
Parallel.ForEach
? That will create new thread for each function call right?How much runs in parallel depends on the MaxDegreeOfParallelism setting and possibly other things.
There is a lot of good info here
https://learn.microsoft.com/en-us/previous-versions/msp-n-p/ff963552(v=pandp.10)