Async and parallel patterns andapplication designArie LeeuwesteijnTechnology Specialist Macawarie@macaw.nl(@arieleeuw)
In this Session…•   Threads and Tasks•   Task Parallel Library•   C# 5.0 async await•   Concurrent Collection Classes•   R...
Objectives• Understanding Async and Parallelism• Patterns, Frameworks and tools  – Purpose  – Scenarios  – Combinations an...
Why Async and Parallel?• Multicore is the rule   – Keep your cores busy   – Used them in an efficient way• Client   – User...
Question• What’s the difference between Async  and Parallel?• Parallel : Having the work done by multiple  workers at the ...
History• .NET 1.0   – Asynchronous Programming Model, APM• .NET 2.0   – Event-Based Programming Model, EPM• .NET 4.0   – T...
Asynchronous Programming ModelWebRequest client = WebRequest.CreateHttp(url);client.BeginGetResponse(Callback, client);pri...
Asynchronous Programming Model• BeginMethod, EndMethod pattern• Complex code  – Error handling  – Object lifetime  – Nesti...
Event-based ProgrammingModel = new WebClient();var clientclient.DownloadStringCompleted   += DownloadCompleted;private voi...
Event-based ProgrammingModel and EventHandlers• Events• Complex code  – Error handling  – Inconsistent synchronization con...
Tasks• Task and Task<T>  – A promise to do something or return a value  – Does not say when or how• Task != Thread  – A ta...
Why use Tasks• Simplifies code• One object to access to get the  – Result, Status and possible Errors• Composition  – Cont...
Creating a Task// Using the Task constructorvar task1 = new Task<int>(() => {   Thread.Sleep(1000);   return 42;});       ...
TaskScheduler• Handles low level queuing of Tasks to Threads• Uses lightweight TreadPool  – Number of cores  – Default sch...
TaskCreationOptions• Task creation options to hint  TaskScheduler  – AttachedToParent  – HideScheduler  – LongRunning  – P...
Task Scheduler                                                            LongRunning                                     ...
Outer task is executed byUsing PreferFairness                          thread from threadpoolTask<int> outer = Task<int>.F...
ConcurrentExclusiveSchedulerPair• Provides task schedulers that coordinate  to execute tasks• Scheduler properties  – Conc...
SchedulerPairsvar schedPair =       new ConcurrentExclusiveSchedulerPair();// Tasks on this scheduler may run concurrently...
How to Cancel a Task• Tasks does not have Stop Method• CancelationTokenSource• -Create CancelationToken and pass to Task  ...
Cancelling a Taskvar tokenSource2 = new CancellationTokenSource();var ct = tokenSource2.Token;var task = Task.Factory.Star...
Error and Exception Handling• Exceptions occurred in execution of  task thrown when accessing the Result• System.Aggregate...
Task Parallel Library• Task• Parallel  – Parallel.For, Parallel.ForEach, Invoke• PLINQ  – Parallel extensions for LINQ  – ...
Stopping Parallel Loopsvar cancellationSource = new CancellationTokenSource();     Using C#var options = new ParallelOptio...
Parallel Options for Loops• CancelationToken  – Stopping all Threads in the loop• MaxDegreeOfParallelism  – If you need mo...
PLINQvar query =  (from d in data   where …   select d).AsParallel();query.ForAll((d) => DoWork(d));
Concurent collections• Set of thread safe collection classes• System.Collections.Concurrent  – ConcurrentBag<T>  – Concurr...
ConcurrentDictionaryvar dict = new ConcurrentDictionary<string, string>();var key =   "key01";var value = "value01";// Get...
C# 5.0• New keywords for async  async marks method or lambda  expression as async  await suspends execution of method unti...
C# 5.0public int LongRunningMethod(){   Task.Delay(1000); Task to return                Wait for   var result = Control 2;...
RunThe story of two Tasks (1)await DoAsync("Task-1");await DoAsync("Task-2");Async but not parallel
RunThe story of two Tasks (2)var t1 = DoAsync("Task-1");var t2 = DoAsync("Task-2");Parallel but returns immediately
RunThe story of two Tasks (3)var t1 = DoAsync("Task-1");var t2 = DoAsync("Task-2");await t1;await t2;Parallel and wait for...
The story of two Tasks (4)var t1 = DoAsync("Task-1");var t2 = DoAsync("Task-2");await Task.WhenAll(t1,t2)Parallel and wait...
Where await can’t be used• catch and finally blocks• lock blocks• LINQ query expressions  – Allowed in extension methods (...
How does await work• Does the C# compiler depends on .NET Task class  for await keyword ?• Compiler needs GetAwaiter() met...
Demo• Implementing an awaitable class  – See what the compiler needs for await  – Just for demo purposes
Make something async usingTask• Simply create a new Task to do the workpublic static async Task<int> MyAsync(){   var resu...
Tasks without Threads• Task construction always take code to  execute on threadpool• But what if you…  –   Already have a ...
TaskCompletionSource• Create Task objects that dont execute  code.var tcs = new TaskCompletionSource<int>();return tcs.Task;
Demo await anything• What you’ll see..  – await oneSecond;  – await techDays;  – await DayOfWeek.Saturday;  – return await...
Task Interop patterns • Why   – Unified Task-based programming model   – Composition • Asynchronous to Task-based applicat...
Async to Task-basedWebRequest wr =     WebRequest.CreateHttp(url);var t = Task<Stream>.Factory.FromAsync(           wr.Beg...
Event to Tasks-based (1)public static Task<string> DownloadStringAsync(Uri url){    var wc = new WebClient();    var tcs =...
Event to Task-based (2)private Task<string> LoadDocumentAsync(WebBrowser browser, string url){   var tcs = new TaskComplet...
Using Lazy<T> with async• Lazy<T> is thread safe and supports async// sync version   async versionvar lazy = new=Lazy<stri...
Using Lazy<T> with asyncvar lazyAsync = new Lazy<Task<string>>(async () =>{    Console.WriteLine("Im only called once...")...
Building an async cachepublic class AsyncCache<TKey, TValue>{    private Func<TKey,Task<TValue>> factory;    private Concu...
SynchronizationContext• Used when code needs specific context  – e.g. updating UI  – SynchronizationContext class is abstr...
SynchronizationContextprivate void Button1Click(object sender,EventArgs e){   var ctx = SynchronizationContext.Current;   ...
Windows8• WinRT is based on COM• Async based on interfaces like this  – IAsyncAction  – IAsyncOperation<T>• Task nor CLR d...
Win8• How can we call these new asynch methods from  .NET or use Tasks in Win8 applications?• WindowsRuntimeSystemExtensio...
Async and ASP.NET• Async controller methods  – Don’t block your IIS worker threads  – Avoid 503 (Server too busy) errors• ...
Async in MVC3public class MVC3Controller : AsyncController{    public void IndexAsync(){       AsyncManager.OutstandingOpe...
Async in MVC4public class MVC4Controller : Controller{   public async Task<ActionResult> Index() {      var client = new W...
Async service proxies
Parallel programming models• .NET 4.0  – Here’s the data, now setup computing  – Primitives for Tasks and Data parallelism...
Enumerables andObservables                IEnumerator<T>                IEnumerable<T>                 IObserver<T>       ...
Reactive Extensions and TPLDataflow• Reactive Extension (Rx)   – Coordination and composition of event streams   – LINQ-ba...
Reactive Extensions Rx• Library for composing asynchronous  and event-based programs using  observable sequences and LINQ-...
Reactive Extensionsvar observable1 = Observable.Range(1, 20);var subscription1 = observable1.Subscribe<int>(Console.WriteL...
Reactive Extensionsvar mouseMove = Observable  .FromEventPattern<MouseEventHandler, MouseEventArgs>(     h => this.MouseMo...
Reactive Extensions• Many operators  – Skip, Take, Zip, Throttle, Buffer, Repeat…• ObserveOn() and SubscribeOn()  methods ...
TPL Dataflow• Primitives for in-process message/data  passing  – Blocks for buffering and processing data• Linkable to for...
Executor Blocks
Buffering Blocks
Join Blocks
Building a Dataflow network                            transform1.LinkTo(join.Target1);                            transfo...
ActionBlock in actionvar actionBlock = new ActionBlock<int>((i) =>  {   Console.WriteLine("[{0}]t{1}",     Thread.CurrentT...
ActionBlock in actionvar actionBlock = new ActionBlock<int>((i) =>  {    Console.WriteLine("[{0}]t{1}",       Thread.Curre...
Linking Blocksvar actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i));var transformBlock = new TransformBlock<...
Bufferingvar actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i));var bufferBlock = new BufferBlock<int>(new Da...
How about JavaScript?• Use async in the browser  – Reactive Extensions for JavaScript  – jQuery Defered and Promises
Thank you…Arie Leeuwesteijnarie@macaw.nl@arieleeuwhttp://tinyurl.com/c78tn5j
Async and parallel patterns and application design - TechDays2013 NL
Upcoming SlideShare
Loading in...5
×

Async and parallel patterns and application design - TechDays2013 NL

3,782

Published on

TechDays2013 NL session on async and parallel programming. Gives an overview of todays relevant .net technologies, examples and tips and tricks. This session will help you to understand and select and use the right async/parallel technology to use in your .net application. (arie@macaw.nl)

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,782
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
51
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Async and parallel patterns and application design - TechDays2013 NL

  1. 1. Async and parallel patterns andapplication designArie LeeuwesteijnTechnology Specialist Macawarie@macaw.nl(@arieleeuw)
  2. 2. In this Session…• Threads and Tasks• Task Parallel Library• C# 5.0 async await• Concurrent Collection Classes• Reactive Extensions• TPL Dataflow• ASP.NET, MVC, Win8
  3. 3. Objectives• Understanding Async and Parallelism• Patterns, Frameworks and tools – Purpose – Scenarios – Combinations and Integrations – Real world examples
  4. 4. Why Async and Parallel?• Multicore is the rule – Keep your cores busy – Used them in an efficient way• Client – User experience• Server – Scalability – Handling requests• Real life is async and parallel
  5. 5. Question• What’s the difference between Async and Parallel?• Parallel : Having the work done by multiple workers at the same time• Async: Use workers efficiently, don’t let them wait for slow actions to complete
  6. 6. History• .NET 1.0 – Asynchronous Programming Model, APM• .NET 2.0 – Event-Based Programming Model, EPM• .NET 4.0 – Task-based Programming Model, TAP• .NET 4.5 – C# 5.0, async, await(*)(*) Async Targeting Pack for Visual Studio 2012 to target .NET 4.0
  7. 7. Asynchronous Programming ModelWebRequest client = WebRequest.CreateHttp(url);client.BeginGetResponse(Callback, client);private void Callback(IAsyncResult ar){ WebRequest client =(WebRequest)ar.AsyncState; WebResponse resp = client.EndGetResponse(ar);}
  8. 8. Asynchronous Programming Model• BeginMethod, EndMethod pattern• Complex code – Error handling – Object lifetime – Nesting async calls• Synchronization context
  9. 9. Event-based ProgrammingModel = new WebClient();var clientclient.DownloadStringCompleted += DownloadCompleted;private void DownloadCompleted( object sender, DownloadStringCompletedEventArgs e) { string resp = e.Result;}client.DownloadStringAsync(uri);
  10. 10. Event-based ProgrammingModel and EventHandlers• Events• Complex code – Error handling – Inconsistent synchronization context• Possible Memory leaks – += handler keeps reference – Dangling handlers
  11. 11. Tasks• Task and Task<T> – A promise to do something or return a value – Does not say when or how• Task != Thread – A task is something you want to be done – A thread is a possible worker to perform that task – A task may not even need a thread
  12. 12. Why use Tasks• Simplifies code• One object to access to get the – Result, Status and possible Errors• Composition – ContinueWith, WhenAll, WhenAny• Task Cancelation
  13. 13. Creating a Task// Using the Task constructorvar task1 = new Task<int>(() => { Thread.Sleep(1000); return 42;}); This is the work to be donetask1.Start();var res = task1.Result;// Using the TaskFactoryvar task2 = new TaskFactory<int>().StartNew(() => { Thread.Sleep(1000); return 42;});
  14. 14. TaskScheduler• Handles low level queuing of Tasks to Threads• Uses lightweight TreadPool – Number of cores – Default scheduler proviced by .NET – TaskScheduler.FromCurrentSynchronizationContext()• Can specify alternative scheduler for task to run on – Task.Start()
  15. 15. TaskCreationOptions• Task creation options to hint TaskScheduler – AttachedToParent – HideScheduler – LongRunning – PreferFairness – A few more..
  16. 16. Task Scheduler LongRunning PreferFairness Work Stealing Task option Local Queue Task option Global Queue (FIFO) Run created by work outside Task on standalone taskif task in Idle threads by tasks placed in Taskscreatedstealrunning no tasks in Instead global global queue Queue local Queue (LIFO) Local or of threadpool Threadpool Worker Worker Worker Worker Thread 1 Thread 2 Thread 3 Thread n = Running Task = Queued Task
  17. 17. Outer task is executed byUsing PreferFairness thread from threadpoolTask<int> outer = Task<int>.Factory.StartNew( () => { Creates 2 more inner var cts = new CancellationTokenSource(); tasks, queued in local var token = cts.Token; task LIFO queue var tasks = new Task<int>[]{ Task.Factory.StartNew(() => { methodA(token); }, token), Task.Factory.StartNew(() => { { methodB(token); }, token) Task.Factory.StartNew(() => methodA(token); }, token, }; TaskCreationOptions.PreferFairness), Task.Factory.StartNew(() => { methodB(token); }, token), var winnerIndex = Task.WaitAny(tasks); TaskCreationOptions.PreferFairness),, cts.Cancel(); On a busy system one of return tasks[winnerIndex].Result;}); the tasks might neverreturn outer.Result; execute (no workstealing by other thread)
  18. 18. ConcurrentExclusiveSchedulerPair• Provides task schedulers that coordinate to execute tasks• Scheduler properties – ConcurrentScheduler • Schedule tasks to this pair that may run concurrently with other tasks on this pair – ExclusiveScheduler • Schedule tasks to this pair that must run exclusively with regards to other tasks on this pair
  19. 19. SchedulerPairsvar schedPair = new ConcurrentExclusiveSchedulerPair();// Tasks on this scheduler may run concurrentlyreaderTask.Start(schedPair.ConcurrentScheduler);// Tasks on this scheduler run exclusivlywriterTask.Start(schedPair.ExclusiveScheduler);
  20. 20. How to Cancel a Task• Tasks does not have Stop Method• CancelationTokenSource• -Create CancelationToken and pass to Task – Signal a Task to Stop – Task should check token for cancelation – Return or Throw Exception• Additional options for timed cancellations etc.• Can also be used for (timed) Wait actions
  21. 21. Cancelling a Taskvar tokenSource2 = new CancellationTokenSource();var ct = tokenSource2.Token;var task = Task.Factory.StartNew(() => { while (true) { if (ct.IsCancellationRequested) ct.ThrowIfCancellationRequested(); // Do Work } }, ct );tokenSource2.Cancel();
  22. 22. Error and Exception Handling• Exceptions occurred in execution of task thrown when accessing the Result• System.AggregateException – InnerExceptions try { var result = task.Result; } catch( AggregateException ex ) { foreach (var e in ex.InnerExceptions){ // Exception handling here }
  23. 23. Task Parallel Library• Task• Parallel – Parallel.For, Parallel.ForEach, Invoke• PLINQ – Parallel extensions for LINQ – AsParallel()• TPL Dataflow
  24. 24. Stopping Parallel Loopsvar cancellationSource = new CancellationTokenSource(); Using C#var options = new ParallelOptions(); Breakoptions.CancellationToken = cancellationSource.Token; UsingParallel.For(0, 10, options, (a, loopState) => loopstate{ . . . . // cancellationToken.Cancel can be called externally // or Break can be called directly if condition is true if (cancellationToken.IsCancellationRequested) loopState.Break(); // loopState.Stop(); . . . .}); Throws OperationCanceledException
  25. 25. Parallel Options for Loops• CancelationToken – Stopping all Threads in the loop• MaxDegreeOfParallelism – If you need more or less Threads• TaskScheduler
  26. 26. PLINQvar query = (from d in data where … select d).AsParallel();query.ForAll((d) => DoWork(d));
  27. 27. Concurent collections• Set of thread safe collection classes• System.Collections.Concurrent – ConcurrentBag<T> – ConcurrentDictionary<TKey, Tvalue> – ConcurrentQueue<T> – ConcurrentStack<T>
  28. 28. ConcurrentDictionaryvar dict = new ConcurrentDictionary<string, string>();var key = "key01";var value = "value01";// Get a valuedict.GetOrAdd(key, k => GetValue(key));// Update or add a valuevalue = "newvalue";dict.AddOrUpdate(key, value, (k, o) => value);
  29. 29. C# 5.0• New keywords for async async marks method or lambda expression as async await suspends execution of method until the awaited tasks returns• Compiler does generate all the code to make this happen
  30. 30. C# 5.0public int LongRunningMethod(){ Task.Delay(1000); Task to return Wait for var result = Control 2;returned to caller 21 * is return result;}public async Task<int> LongRunningMethodAsync(){ await Task.Delay(1000); var result = 21 * 2; return result; This code runs when Task returns}
  31. 31. RunThe story of two Tasks (1)await DoAsync("Task-1");await DoAsync("Task-2");Async but not parallel
  32. 32. RunThe story of two Tasks (2)var t1 = DoAsync("Task-1");var t2 = DoAsync("Task-2");Parallel but returns immediately
  33. 33. RunThe story of two Tasks (3)var t1 = DoAsync("Task-1");var t2 = DoAsync("Task-2");await t1;await t2;Parallel and wait for both tasks to return
  34. 34. The story of two Tasks (4)var t1 = DoAsync("Task-1");var t2 = DoAsync("Task-2");await Task.WhenAll(t1,t2)Parallel and wait for both tasks to returnBut more efficient
  35. 35. Where await can’t be used• catch and finally blocks• lock blocks• LINQ query expressions – Allowed in extension methods (. syntax)• Unsafe code
  36. 36. How does await work• Does the C# compiler depends on .NET Task class for await keyword ?• Compiler needs GetAwaiter() method• Method must return a class that implements – GetResult() – IsCompleted() – INotifyCompleted.OnCompleted• Implemented in Task
  37. 37. Demo• Implementing an awaitable class – See what the compiler needs for await – Just for demo purposes
  38. 38. Make something async usingTask• Simply create a new Task to do the workpublic static async Task<int> MyAsync(){ var result = await new Task<int>(() => { // your long running code code here... var calculatedResult = LongCalculation(); return calculatedResult; }); return result;}
  39. 39. Tasks without Threads• Task construction always take code to execute on threadpool• But what if you… – Already have a thread – Don’t need a thread – Waiting for async IO, network etc – Handle events
  40. 40. TaskCompletionSource• Create Task objects that dont execute code.var tcs = new TaskCompletionSource<int>();return tcs.Task;
  41. 41. Demo await anything• What you’ll see.. – await oneSecond; – await techDays; – await DayOfWeek.Saturday; – return await Process.Start("HelloWorld.exe");
  42. 42. Task Interop patterns • Why – Unified Task-based programming model – Composition • Asynchronous to Task-based application model – Factory.TaskFromAsync • Event-basedto Task-based application model – TaskCompletionSource
  43. 43. Async to Task-basedWebRequest wr = WebRequest.CreateHttp(url);var t = Task<Stream>.Factory.FromAsync( wr.BeginGetRequestStream, wr.EndGetRequestStream, null );
  44. 44. Event to Tasks-based (1)public static Task<string> DownloadStringAsync(Uri url){ var wc = new WebClient(); var tcs = new TaskCompletionSource<string>(); wc.DownloadStringCompleted += (s,e) =>{ if (e.Error != null ) tcs.TrySetException(e.Error); else if (e.Cancelled) tcs.TrySetCanceled(); else tcs.TrySetResult(e.Result) } Task complete wc.DownloadStringAsync(url); return tcs.Task;}
  45. 45. Event to Task-based (2)private Task<string> LoadDocumentAsync(WebBrowser browser, string url){ var tcs = new TaskCompletionSource<string>(); WebBrowserDocumentCompletedEventHandler handler = null; Event source lifetime handler = (sender, managed externally e) => { tcs.SetResult(browser.DocumentText); browser.DocumentCompleted -= handler; }; browser.DocumentCompleted += handler; browser.Url = new Uri(url); Need to unregister return tcs.Task; handler}
  46. 46. Using Lazy<T> with async• Lazy<T> is thread safe and supports async// sync version async versionvar lazy = new=Lazy<string>(() => lazyAsync new Lazy<Task<string>>(async () => {{ Thread.Sleep(5000); await Task.Delay(5000); Factory code return DateTime.Now.ToString();});});var value = lazy.Value;var value = await lazyAsync.Value;
  47. 47. Using Lazy<T> with asyncvar lazyAsync = new Lazy<Task<string>>(async () =>{ Console.WriteLine("Im only called once..."); await Task.Delay(5000); return DateTime.Now.ToString();});// Starts and wait for taskvar t1 = lazyAsync.Value;// Waits for same taskvar t2 = lazyAsync.Value;
  48. 48. Building an async cachepublic class AsyncCache<TKey, TValue>{ private Func<TKey,Task<TValue>> factory; private ConcurrentDictionary<TKey,Lazy<Task<TValue>>> dict;var cache = new AsyncCache<string, string>( async k => public AsyncCache(Func<TKey, Task<TValue>> valueFactory){ { factory = valueFactory; dictwait Task.Delay(1000); = new ConcurrentDictionary<TKey, Lazy<Task<TValue>>>(); } return DateTime.Now.ToString(); }); public Task<TValue> this[TKey key] { get { return dict.GetOrAdd(key,var t = await cache["key1"]; toAdd => new Lazy<Task<TValue>>(() => factory(toAdd))).Value; } }}
  49. 49. SynchronizationContext• Used when code needs specific context – e.g. updating UI – SynchronizationContext class is abstract – Static Current property returns relevant instance (Post is specific) Ensures one at a time Dispatcher.BeginInvoke Control.BeginInvoke
  50. 50. SynchronizationContextprivate void Button1Click(object sender,EventArgs e){ var ctx = SynchronizationContext.Current; new TaskFactory().StartNew(() => { ctx.Post(state => { label1.Text = string.Format("Update from Task") }, null});}• await Task continues automatically on Current context
  51. 51. Windows8• WinRT is based on COM• Async based on interfaces like this – IAsyncAction – IAsyncOperation<T>• Task nor CLR do not implement these interfaces
  52. 52. Win8• How can we call these new asynch methods from .NET or use Tasks in Win8 applications?• WindowsRuntimeSystemExtensions methods take care of conversions public static Task AsTask<TResult>( this IAsyncOperation<TResult> source); public static IAsyncOperation<TResult> AsAsyncOperation<TResult>(this Task<TResult> source);
  53. 53. Async and ASP.NET• Async controller methods – Don’t block your IIS worker threads – Avoid 503 (Server too busy) errors• MVC4/.NET4.5 supports async/await – Possible in MVC3, but more complex – WebForms also have support for async• Always use async proxies when calling external services
  54. 54. Async in MVC3public class MVC3Controller : AsyncController{ public void IndexAsync(){ AsyncManager.OutstandingOperations.Increment(1); Task.Factory.StartNew(() =>{ var client = new WebClient(); string reply = client.DownloadString(uri); AsyncManager.Parameters["data"] = reply; AsyncManager.OutstandingOperations.Decrement();}); } public ActionResult IndexCompleted(string data){ this.ViewBag.Result = data; return View(); }}
  55. 55. Async in MVC4public class MVC4Controller : Controller{ public async Task<ActionResult> Index() { var client = new WebClient(); this.ViewBag.Result = await client.DownloadStringTaskAsync(uri); return View(); }}
  56. 56. Async service proxies
  57. 57. Parallel programming models• .NET 4.0 – Here’s the data, now setup computing – Primitives for Tasks and Data parallelism• The Inverse model – Setup the Computing, now here’s the data – Primitives for Dataflow parallelism
  58. 58. Enumerables andObservables IEnumerator<T> IEnumerable<T> IObserver<T> IObservable<T>Current{get;} GetEnumerator() OnNext(T) IDisposable Subscribe(IObserver<T>)MoveNext() OnError(Ex) Reset() OnCompled() Iterations, pull model Subscription, pushmodel
  59. 59. Reactive Extensions and TPLDataflow• Reactive Extension (Rx) – Coordination and composition of event streams – LINQ-based API• Dataflow (TDF) – Building blocks for message passing and parallelizing – Explicit control over how data is buffered and moved• Many similarities, but each address distinct needs
  60. 60. Reactive Extensions Rx• Library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. – Rx for .NET, Rx.NET – Rx for JavaScript, RxJS – Rx for Windows Phone
  61. 61. Reactive Extensionsvar observable1 = Observable.Range(1, 20);var subscription1 = observable1.Subscribe<int>(Console.WriteLine);var oneSecond = TimeSpan.FromSeconds(1);var observable2 = Observable.Timer(oneSecond,oneSecond);var subscription2 = observable2.Subscribe<int>(Console.WriteLine);var observer3 = observable1 .Select((i) => i) .Skip(2) // skip first two values .Where(i => (i % 2 == 0)) // only get the even values .Zip(observable2, (i, t) => i) // one value per second .Subscribe((i) => Console.WriteLine(i));
  62. 62. Reactive Extensionsvar mouseMove = Observable .FromEventPattern<MouseEventHandler, MouseEventArgs>( h => this.MouseMove += h, h => this.MouseMove -= h);var mouseUp = Observable .FromEvent<MouseEventHandler, MouseEventArgs>( h => this.MouseUp += h, h => this.MouseUp -= h);var mouseDown = Observable .FromEvent<MouseEventHandler, MouseEventArgs>( h => this.MouseDown += h, h => this.MouseDown -= h);var observable = mouseMove // Get mousemove positions .SkipUntil(mouseDown) // Skip until mouse button down .TakeUntil(mouseUp) // Take until mouse button is up .Select(a => a.EventArgs.Location);
  63. 63. Reactive Extensions• Many operators – Skip, Take, Zip, Throttle, Buffer, Repeat…• ObserveOn() and SubscribeOn() methods – Optional Scheduler and Context parameters – Specify which thread/context the observer and subscribers run on
  64. 64. TPL Dataflow• Primitives for in-process message/data passing – Blocks for buffering and processing data• Linkable to form a network – Data automatically propagated from sources to linked targets – Enables building powerful parallel and asynchronous pipelinesBased• Integrates with Task, IObservable,…
  65. 65. Executor Blocks
  66. 66. Buffering Blocks
  67. 67. Join Blocks
  68. 68. Building a Dataflow network transform1.LinkTo(join.Target1); transform2.LinkTo(join.Target2);input.LinkTo(action1); join.LinkTo(action2);input.LinkTo(transform1);input.LinkTo(transform2);
  69. 69. ActionBlock in actionvar actionBlock = new ActionBlock<int>((i) => { Console.WriteLine("[{0}]t{1}", Thread.CurrentThread.ManagedThreadId,i); });for (var i = 0; i < 10; i++) actionBlock.Post(i);
  70. 70. ActionBlock in actionvar actionBlock = new ActionBlock<int>((i) => { Console.WriteLine("[{0}]t{1}", Thread.CurrentThread.ManagedThreadId,i); }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 4 }); Max 4 instancesfor (var i = 0; i < 10; i++) running actionBlock.Post(i);
  71. 71. Linking Blocksvar actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i));var transformBlock = new TransformBlock<int, int>((i) => i * i);transformBlock.LinkTo(actionBlock);for (var i = 0; i < 10; i++) transformBlock.Post(i);
  72. 72. Bufferingvar actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i));var bufferBlock = new BufferBlock<int>(new DataflowBlockOptions( { BoundedCapacity = 10 }); Post blocks if buffer is fullbufferBlock.LinkTo(actionBlock);
  73. 73. How about JavaScript?• Use async in the browser – Reactive Extensions for JavaScript – jQuery Defered and Promises
  74. 74. Thank you…Arie Leeuwesteijnarie@macaw.nl@arieleeuwhttp://tinyurl.com/c78tn5j
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×