Don’t await … try async/await !

20
Don’t await … try async / await ! Andrea Ceroni [email protected] @andrekiba6 Sponsored by 10 marzo 2016 Relatore

Transcript of Don’t await … try async/await !

Page 1: Don’t await … try async/await !

Don’t await … try async/await !

Andrea [email protected]

@andrekiba6

Sponsored by

10 marzo 2016

Relatore

Page 2: Don’t await … try async/await !

Introduzione ad async/awaitBest practices di programmazione asincrona

Tipo di sessione: Frontale

Durata sessione: 55min

Page 3: Don’t await … try async/await !

Argomenti

Concorrenza

Thread e Task

CPU-bound vs IO-bound

APM e EAP …la preistoria!

async/await

Async best practices

1/18

Page 4: Don’t await … try async/await !

Code Q & AIntro

2/18

Page 5: Don’t await … try async/await !

3/18Cosa è asincrono ?

significa“non nello stesso tempo”

non significa“in background”

Page 6: Don’t await … try async/await !

4/18Async event-driven

sveglia colazione

Page 7: Don’t await … try async/await !

5/18

Task

Page 8: Don’t await … try async/await !

6/18

TaskIO-bound

Page 9: Don’t await … try async/await !

7/18

TaskCPU-bound

Page 10: Don’t await … try async/await !

8/18

Concorrenteinterleaved

Parallelosimultaneo

Page 11: Don’t await … try async/await !

9/18

Continuazionefunzione

Page 12: Don’t await … try async/await !

10/18

OldCode().Wait();

Page 13: Don’t await … try async/await !

11/18

cattura il contesto

sembra di scrivere codice sincrono il lavoro “sporco” è

svolto dal compilatore

async / await

Page 14: Don’t await … try async/await !

12/18

async await keywordspublic async Task DoSomethingAsync(){

await Task.Delay(1000);Console.WriteLine(“ciao!”);

}

async abilita l’uso di await e avverte il compilatore che il metodo sarà trattato in modo “speciale”

await indica un punto di sospensione, qui il flusso diventa effettivamente asincronoprende un awaitable (operazione asincrona) e verifica se è già completa

se è completa il flusso continua in modo sincronoaltrimenti chiede all’awaitable di eseguire il resto del metodo quando avrà terminato e ritorna

quando awaitable termina il resto del metodo viene eseguito nel contesto catturato

Page 15: Don’t await … try async/await !

13/18

Task typepublic async Task DoSomethingAsync(){

await Task.Delay(1000);Console.WriteLine(“ciao!”);

}

In .NET esistono due awaitable già pronti, Task e Task<T>

N.B. è il tipo che è awaitable, non il metodo che ritorna il tipo!

Un metodo asincrono può ritornare Task, Task<T> oppure void

N.B. un metodo asincrono attende una sola operazione alla volta e un Task è completo una sola volta!

public async Task<int> DoSomethingAsync(){

await Task.Delay(1000);return 6;

}

Page 16: Don’t await … try async/await !

14/18

Contesto1. se siamo sullo UI thread è UI context2. se stiamo rispondendo ad una request ASP.NET, è il request context3. altrimenti in quasi tutti gli altri casi è il thread pool context (TaskScheduler.Default)

//WPFpublic async void DownloadButton_Click(object sender, EventArgs e){

await DownloadFileAsync(...);resultTextBox.Text = "File downloaded!";

}

//ASP.NETpublic async void DownloadButton_Click(object sender, EventArgs e){

await DownloadFileAsync(...);Response.Write("File downloaded!");

}

Page 17: Don’t await … try async/await !

15/18

await NewCode();

Page 18: Don’t await … try async/await !

16/18

Riassunto

Task direttamente per operazioni IO-bound

Task.Run per operazioni CPU-bound

async Task invece di async void

Async all the way, non bloccare codice asincrono

ConfigureAwait(false) se il contesto non è importante

Page 19: Don’t await … try async/await !

17/18

await Q;if(iKnowTheAnswer)

A; J

Page 20: Don’t await … try async/await !

18/18

Grazie!