Presentation is loading. Please wait.

Presentation is loading. Please wait.

12 Asynchronous Programming

Similar presentations


Presentation on theme: "12 Asynchronous Programming"— Presentation transcript:

1 12 Asynchronous Programming
.NET and .NET Core 12 Asynchronous Programming Pan Wuming 2017

2 Topics Asynchronous programming Tasks for an I/O-Bound Operation
Tasks for a CPU-Bound Operation The Asynchronous Programming Model async Programming in F#

3 Asynchronous programming
It is a key technique that makes it straightforward to handle: blocking I/O and concurrent operations on multiple cores. It is not HPC or Parallel Programming Typically a task is decomposed into many parts running on different computing node. Parallel programming in the .NET Framework The .NET Framework provides three patterns for performing asynchronous operations. Only Task-based Asynchronous Pattern (TAP) is recommended now.

4 Task-based async APIs and the language-level asynchronous programming
Handles more server requests by yielding threads to handle more requests while waiting for I/O requests to return. Enables UIs to be more responsive by yielding threads to UI interaction while waiting for I/O requests and by transitioning long- running work to other CPU cores. Many of the newer .NET APIs are asynchronous. It's easy to write async code in .NET!

5 Task and Task<T>
Promise Model of Concurrency Task represents a single operation which does not return a value. Task<T> represents a single operation which returns a value of type T . Tasks are abstractions of work happening asynchronously, and not abstractions over threading. The OS is essentially asynchronous through interrupts. The await keyword, provides a higher-level abstraction for using tasks.

6 Tasks for an I/O-Bound Operation
Threads are freed up when the I/O-bound work starts, rather than when it finishes.

7 public Task<string> GetHtmlAsync()
{ // Execution is synchronous here var client = new HttpClient(); return client.GetStringAsync(" }

8 public async Task<string> GetFirstCharactersCountAsync(string url, int count)
{ var client = new HttpClient(); var page = await client.GetStringAsync(" // Execution resumes when the client.GetStringAsync task completes, // becoming synchronous again. if (count > page.Length) return page; else return page.Substring(0, count); }

9 Tasks for a CPU-Bound Operation
They can be used to keep the caller of the async method responsive. This does not provide any protection for shared data.

10 public async Task<int> CalculateResult(InputData data)
{ // This queues up the work on the threadpool. var expensiveResultTask = Task.Run(() => DoExpensiveCalculation(data)); // Note that at this point, you can do some other work concurrently, // as CalculateResult() is still executing! // Execution of CalculateResult is yielded here! var result = await expensiveResultTask; return result; }

11 Key Pieces to Understand
Async code can be used for both I/O-bound and CPU-bound code, but differently for each scenario. Async code uses Task<T> and Task , which are constructs used to model work being done in the background. The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.

12 Control Flow

13 async Modifier Use the async modifier to specify that a method, lambda expression, or anonymous method is asynchronous. If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously. An async method can have a return type of Task, Task<TResult>, or void. You use the void return type primarily to define event handlers.

14 The Asynchronous Model
For I/O-bound code, you await an operation which returns a Task or Task<T> inside of an async method. For CPU-bound code, you await an operation which is started on a background thread with the Task.Run method.

15 I/O-Bound Example: Downloading data from a web service
Async Lambdas private readonly HttpClient _httpClient = new HttpClient(); downloadButton.Clicked += async (o, e) => { var stringData = await _httpClient.GetStringAsync(URL); DoSomethingWithData(stringData); };

16 CPU-bound Example: Performing a Calculation for a Game
private DamageResult CalculateDamageDone() { // Code omitted: } calculateButton.Clicked += async (o, e) => var damageResult = await Task.Run(() => CalculateDamageDone()); DisplayDamage(damageResult); };

17 Waiting for Multiple Tasks to Complete
Task.WhenAll and Task.WhenAny allow you to write asynchronous code which performs a non-blocking wait on multiple background jobs.

18 public async Task<User> GetUserAsync(int userId)
{ // Code omitted: } public static async Task<IEnumerable<User>> GetUsersAsync(IEnumerable<int> userIds) var getUserTasks = new List<Task<User>>(); foreach (int userId in userIds) getUserTasks.Add(GetUserAsync(userId)); return await Task.WhenAll(getUserTasks);

19 In F# The core of async programming in F# is Async<‘T> , a representation of work that can be triggered to run in the background. let! use! do! return return!

20 let fetchHtmlAsync url =
let uri = Uri(url) use webClient = new WebClient() let! html = webClient.AsyncDownloadString(uri) return html } let html = " |> fetchHtmlAsync |> Async.RunSynchronously printfn "%s" html

21 Case Study The chain of asyncs The async event

22

23

24

25

26

27

28 Most Responsive!


Download ppt "12 Asynchronous Programming"

Similar presentations


Ads by Google