Mais conteúdo relacionado Semelhante a Async/Await (20) Async/Await1. J e f f H a r t
V a n i s h i n g C l o u d s , I n c .
Async/Await for Fun and Profit
Multithreading is just one damn thing after…
…before, or the simultaneously with another.
Scott Meyers and Andrei Alexandrescu
…before
March 7-8, 2015
2. Why Multithreading?
“Modern” apps force multithreading
Desktop/client – avoiding the “toilet bowl”
Server-side scalability – all about the cores
Economics – computers not faster since P4 (90nm)
Copyright © Jeff Hart, 2015
3. Know Your Goal
Offloading – free the “main” thread
Still uses another (Thread Pool) thread
Works for CPU bound (i.e., thread backed)
Scaling – not using “any” thread
Still uses IO completion ports
Only works for IO bound provided by “framework”
Copyright © Jeff Hart, 2015
4. Domain: Prime Numbers
Natural numbers c only divisible by itself and 1
for(int d=2; d<c-1; d++)…
Only even prime is 2
for(int d=3; d<c-1; d+=2)…
If c/d = q, then c/q = d; and q or d ≤ SQRT(c)
for(int d=3; d<=Math.Sqrt(c); d+=2)…
If c/d, then d is a prime or divisible by one<d
So only test against previously found primes
… lots of more powerful sieves
Copyright © Jeff Hart, 2015
5. Basic ROT
await converts Task<T> to <T> (Task to void)
Using await changing signature: async Task[<T>]
Warning on async method w/o using await
Can’t await in catch/finally or lock
Can’t make properties async
Method returns Task<T>, but you return T;
If you have used await
Never call async void (event handlers only)
Copyright © Jeff Hart, 2015
6. Floor to Ceiling
Go down to:
Framework XxxAsync for scaling
“Creating” asynchronicity
Task.Run( { … } ) way better than worker threads
Go up to:
Handler
MSTest, etc.
“Eating” asynchronicity
task.ContinueWith( …, TaskContinuationOptions.Only|Not)
task.Result - blocks
Copyright © Jeff Hart, 2015
7. We’ve Been At This…
CLR v1: APM – Async Programming Model
Simple (mechanical) but limiting
CLR v2: EAP – Event-based Asynchronous Pattern
Very flexible but lots of “namespace noise”
CLR v4: TAP – Task-based Asynchronous Pattern
V4.5 async/await “complier sugar”
Copyright © Jeff Hart, 2015
8. Getting Real (Data)
Entity Framework 6 is TAP enabled
Secret: using System.Data.Entity;
All|AnyAsync
Count|Average|Sum|Min|MaxAsync
ContainsAsync
First|Single[OrDefault]Async
ForEachAsync
LoadAsync
ToArray|List|DictionaryAsync
Copyright © Jeff Hart, 2015
9. Graduation
foreach( var d in GetLotsOfData() ){
DoSomethingReallyLong(d);
}
Parallel.ForEach( GetLotsOfData(), d=>{
DoSomethingReallyLong(d);
}
var tasks = new List<Task>()
foreach( var d in GetLotsOfData() ){
tasks.Add(DoSomethingReallyLongAsync(d));
}
Task.AwaitAll(tasks);
?
Copyright © Jeff Hart, 2015
10. Layers of Multithreading
Async/await two-step
Separating task and await
Knowin’ when to make ‘em;
And knowin’ when to tend ‘em
Rules of Thumb
Mostly compiler enforced/assisted
Avoid async void (fire and forget)
Go “floor to ceiling” when possible
Task’s members to “stop”
TaskCompletionSource to “create”
The scary bit:
Synchronizing (data)
Copyright © Jeff Hart, 2015
11. SpeakerRate.com
• Search for “async” or “socalcodecamp”
• h t t p : / / s p e a k e r r a t e . c o m / t a l k s / 4 9 3 2 1 - s o c a l c o d e c a m p -
i n t r o - t o - a s y n c - a w a i t
Thank YOU!
Copyright © Jeff Hart, 2015
17