async development con visual studio 2012

32
Async Developmen t Raffaele Fanizzi www.vifani.com

Upload: raffaele-fanizzi

Post on 24-Jun-2015

997 views

Category:

Technology


0 download

DESCRIPTION

La nuova versione di Visual Studio introduce il supporto al .NET Framework 4.5, a C# 5 e Visual Basic 11, che con nuove parole chiavi e nuovi compilatori, aprono le porte alla programmazione asincrona Task based. In questa sessione verranno presentate le funzionalità legate a questo pattern, facendo un parallelo con gli approcci che fino ad oggi sono stati impiegati per risolvere i medesimi problemi.

TRANSCRIPT

Page 1: Async Development con Visual Studio 2012

Async DevelopmentRaffaele Fanizziwww.vifani.com

Page 2: Async Development con Visual Studio 2012

Agenda Introduzione Perché la programmazione asincrona? Operazioni I/O bound vs CPU bound Un po di storia

Async Programming Model (APM) Event-based Async Pattern (EAP)

Task-based Asynchronous Pattern (TAP) Flusso di esecuzione dei metodi async API coinvolte

Page 3: Async Development con Visual Studio 2012

Cosa significa «concorrenza»?

La capacità di eseguire o far sembrare in esecuzione due operazioni nello stesso momento

Introduzione

Page 4: Async Development con Visual Studio 2012

Cosa significa «asincrono»?

La capacità di eseguire un’operazione senza che il richiedente resti in attesa della fine della stessa

Introduzione

Page 5: Async Development con Visual Studio 2012

• Le operazioni asincrone possono condividere un singolo thread per eseguire più operazioni

• L’obiettivo è quello di sfruttare al massimo un singolo thread, senza bloccarlo quando è in attesa di un risultato

• Asincrono != Multithreading

Perché la programmazione asincrona?

Page 6: Async Development con Visual Studio 2012

• Tanti task possono essere eseguiti insieme, ma non sempre ogni task ha bisogno di un suo thread

• L’uso spregiudicato di nuovi thread non è sempre la risposta migliore

• I thread sono costosi da creare (1 MB di stack per ogni thread)

• I thread sono costosi da gestire (lock, ReadWriterLockSlim, context switching)

Perché la programmazione asincrona?

Page 7: Async Development con Visual Studio 2012

• Perché siamo obbligati a farlo in alcuni casi

• Per migliorare l’esperienza utente lato client

• Per sfruttare al meglio i worker thread lato server

Perché la programmazione asincrona?

Page 8: Async Development con Visual Studio 2012

• CPU bound: tempo speso per l’esecuzione di elaborazioni complesse (codec video, grafica 3D, FFT, ecc…) => ideali per il multithreading

• I/O bound: tempo speso ad attendere la risposta da periferiche di I/O come il disco, la rete (web server, database server, ecc…) => ideali per la programmazione asincrona

Operazioni I/O bound vs CPU bound

Page 9: Async Development con Visual Studio 2012

• Disponibile fin dal .NET Framework 1.0

• Lo ritroviamo nelle chiamate che cominciano per Begin/End

• Restituiscono un IAsyncResult per verificare lo stato dell’esecuzione

• Callback-based

Async Programming Model (APM)

Page 10: Async Development con Visual Studio 2012

delegate AsyncCallback(IAsyncResult);

interface IAsyncResult {

object AsyncState { get; }

WaitHandle AsyncWaitHandle { get; }

bool CompletedSynchronously { get; }

bool IsCompleted { get; }

}

void BeginXxx(arg1, arg2, ..., AsyncCallback, state);

TResult EndXxx(IAsyncResult);

Async Programming Model (APM)

Page 11: Async Development con Visual Studio 2012

public static void Transfer(string url, Stream streamOut) {

var request = WebRequest.Create(url);

using (var response = request.GetResponse())

{

var streamIn = response.GetResponseStream();

var size = 1024;

var buffer = new byte[size];

while (true)

{

var lengthRead = streamIn.Read(buffer, 0, size);

if (lengthRead <= 0) break;

streamOut.Write(buffer, 0, lengthRead);

}

}

}

Codice sincrono

Page 12: Async Development con Visual Studio 2012

IAsyncResult BeginTransfer(url, streamOut, ..., callback) {

// keep context and BeginGetResponse

}

void EndGetResponse(...) {

// get streamIn and BeginRead

}

void EndRead(...) {

// data read and BeginWrite

}

void EndWrite(...) {

// data wrote and BeginRead

}

Codice asincrono APM

Page 13: Async Development con Visual Studio 2012

public void Transfer(string url, Stream streamOut) { try { var request = WebRequest.Create(url); using (var response = request.GetResponse()) { var streamIn = response.GetResponseStream(); var size = 1024; var buffer = new byte[size]; while (true) { var lengthRead = streamIn.Read(buffer, 0, size); if (lengthRead <= 0) break; streamIn.Write(buffer, 0, lengthRead); } } } catch { // ... } }

Codice sincrono robusto

Page 14: Async Development con Visual Studio 2012

IAsyncResult BeginTransfer(url, streamOut, ..., callback) {

try { ... } catch { ... }

}

void EndGetResponse(...) {

try { ... } catch { ... }

}

void EndRead(...) {

try { ... } catch { ... }

}

void EndWrite(...) {

try { ... } catch { ... }

}

Codice asincrono robusto APM

Page 15: Async Development con Visual Studio 2012

• Disponibile fin dal .NET Framework 1.0

• Lo ritroviamo nelle chiamate con suffisso Async e relativo evento Completed

• Difficile capire lo stato dell’esecuzione

• Callback-based

Event-based Async Pattern (EAP)

Page 16: Async Development con Visual Studio 2012

class XxxCompletedEventArgs : EventArgs { Exception Error { get; } TResult Result { get; }}class Worker { event EventHandler<XxxCompletedArgs> XxxCompleted; void XxxAsync(arg1, arg2, ...);}

Event-based Async Pattern (EAP)

Page 17: Async Development con Visual Studio 2012

Codice asincrono (EAP)

Page 18: Async Development con Visual Studio 2012

• Perché i precedenti pattern asincroni non hanno avuto successo?

• Produttività e pigrizia: bisogna scrivere più righe di codice, più metodi ed è più difficile gestire le eccezioni, il progresso e la cancellazione

• Resistenza psicologica: nella nostra mente identifichiamo una chiamata con un’operazione. Eventi e callback non sono naturalmente accettati

Un po’ di storia

Page 19: Async Development con Visual Studio 2012

Task-based Asynchronous Pattern (TAP) Disponibile fin dal .NET Framework 4, raggiunge piena maturazione nella versione 4.5

Si basa pesantemente sul namespace System.Threading.Task

Si sfrutta al meglio con le parole chiavi supportate dai compilatori C# 5 e VB 11

E’ da considerarsi lo standard di riferimento per tutti i nuovi sviluppi

Page 20: Async Development con Visual Studio 2012

Task-based Asynchronous Pattern (TAP) La TAP si basa su metodi con le seguenti caratteristiche

Inizia con “async [void|Task|Task<T>]” Il nome del metodo finisce con “Async” I parametri possono opzionalmente avere un CancellationToken e un IProgress<T>

async [void|Task|Task<T>] MethodNameAsync(parameters[, CancellationToken ct, IProgress<T> prog])

Page 21: Async Development con Visual Studio 2012

Task-based Asynchronous Pattern (TAP) async – Indica che un metodo ha un punto nel quale può essere interrotto

await – E’ usato prima di un’istanza di tipo Task e sospende l’esecuzione del metodo corrente fino a quando un task non si conclude

Page 22: Async Development con Visual Studio 2012

Flusso di esecuzione dei metodi async I compilatori di C# 5 e VB 11 non fanno altro che trasformare il codice successivo ad un await in una callback del task al quale l’await si riferisce

La prima await causa immediatamente l’uscita dal metodo in esecuzione

Completato il task, l’esecuzione del metodo prosegue normalmente nello stesso SynchronizationContext del chiamante

Page 23: Async Development con Visual Studio 2012

Flusso di esecuzione dei metodi async

Page 24: Async Development con Visual Studio 2012

public static void Transfer(string url, Stream streamOut) {

var request = WebRequest.Create(url);

using (var response = request.GetResponse())

{

var streamIn = response.GetResponseStream();

var size = 1024;

var buffer = new byte[size];

while (true)

{

var lengthRead = streamIn.Read(buffer, 0, size);

if (lengthRead <= 0) break;

streamOut.Write(buffer, 0, lengthRead);

}

}

}

Codice sincrono

Page 25: Async Development con Visual Studio 2012

public static async void Transfer(string url, Stream streamOut) {

var request = WebRequest.Create(url);

using (var response = await request.GetResponseAsync())

{

var streamIn = response.GetResponseStream();

var size = 1024;

var buffer = new byte[size];

while (true)

{

var lengthRead = await streamIn.ReadAsync(buffer, 0, size);

if (lengthRead <= 0) break;

await streamOut.WriteAsync(buffer, 0, lengthRead);

}

}

}

Codice asincrono (TAP)

Page 26: Async Development con Visual Studio 2012

Task-based Asynchronous Pattern (TAP) Esistono alcune limitazioni/regole

I metodi async devono avere al loro interno almeno un await

Niente parametri ref o out

Page 27: Async Development con Visual Studio 2012

API Coinvolte .NET Framework 4.5

System.IO.Stream System.IO.TextReader System.Xml.XmlReader System.Net.Mail System.Net.Http.HttpClient System.Net.WebSockets

Page 28: Async Development con Visual Studio 2012

API Coinvolte ASP.NET MVC 4

Controller con metodi asincroni ADO.NET

DbConnection DbCommand DbDataReader

Windows 8

Page 29: Async Development con Visual Studio 2012

Domande ?

Page 30: Async Development con Visual Studio 2012

Riferimenti Raffaele Fanizzi – Il mio blogwww.vifani.com

Asynchronous Programming with Async and Awaithttp://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

Implementing the Task-based Asynchronous Patternhttp://msdn.microsoft.com/en-us/library/hh873177.aspx

Page 31: Async Development con Visual Studio 2012

Grazie per l’attenzione

Page 32: Async Development con Visual Studio 2012

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.