Async Development con Visual Studio 2012
-
Upload
raffaele-fanizzi -
Category
Technology
-
view
997 -
download
0
description
Transcript of Async Development con Visual Studio 2012
Async DevelopmentRaffaele Fanizziwww.vifani.com
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
Cosa significa «concorrenza»?
La capacità di eseguire o far sembrare in esecuzione due operazioni nello stesso momento
Introduzione
Cosa significa «asincrono»?
La capacità di eseguire un’operazione senza che il richiedente resti in attesa della fine della stessa
Introduzione
• 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?
• 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?
• 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?
• 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
• 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)
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)
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
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
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
IAsyncResult BeginTransfer(url, streamOut, ..., callback) {
try { ... } catch { ... }
}
void EndGetResponse(...) {
try { ... } catch { ... }
}
void EndRead(...) {
try { ... } catch { ... }
}
void EndWrite(...) {
try { ... } catch { ... }
}
Codice asincrono robusto APM
• 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)
class XxxCompletedEventArgs : EventArgs { Exception Error { get; } TResult Result { get; }}class Worker { event EventHandler<XxxCompletedArgs> XxxCompleted; void XxxAsync(arg1, arg2, ...);}
Event-based Async Pattern (EAP)
Codice asincrono (EAP)
• 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
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
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])
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
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
Flusso di esecuzione dei metodi async
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
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)
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
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
API Coinvolte ASP.NET MVC 4
Controller con metodi asincroni ADO.NET
DbConnection DbCommand DbDataReader
Windows 8
Domande ?
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
Grazie per l’attenzione
© 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.