Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Async await...oh wait!

5 240 vues

Publié le

An overview of .NET Async Await (20 minutes)

Publié dans : Logiciels
  • I’ve personally never heard of companies who can produce a paper for you until word got around among my college groupmates. My professor asked me to write a research paper based on a field I have no idea about. My research skills are also very poor. So, I thought I’d give it a try. I chose a writer who matched my writing style and fulfilled every requirement I proposed. I turned my paper in and I actually got a good grade. I highly recommend ⇒ www.HelpWriting.net ⇐
       Répondre 
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici
  • Search Your Car. Gov't Seized Cars - All Makes & Models Up to 95% OFF, 4,000+ Auctions US WIDE, Listings Guaranteed in Your State, You Save Thousands! ➢➢➢ https://w.url.cn/s/Ayy1McS
       Répondre 
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici
  • Sex in your area is here: ♥♥♥ http://bit.ly/39mQKz3 ♥♥♥
       Répondre 
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici
  • Follow the link, new dating source: ♥♥♥ http://bit.ly/39mQKz3 ♥♥♥
       Répondre 
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici

Async await...oh wait!

  1. 1. ASYNC, AWAIT, OH… WAIT! YOU’D BETTER WATCH YOUR STEP December 2016 – Alt.Net Paris @tpierrain (use case driven)
  2. 2. “USE CASE DRIVEN”, BUT ALSO… • Reactive Programmer (> 10 years) • eXtreme Programmer (> 10 years) • Programmer (> 18 years) @tpierrain NFluent Value (DDD)
  3. 3. LET’S START WITH A QUESTION…
  4. 4. LET’S START WITH A QUESTION… Q:What happens line 24, when await occurs?
  5. 5. OUPS! LET’S ADD SOME CONTEXT
  6. 6. LET’S START WITH A QUESTION… Q:What happens line 24, when await occurs?
  7. 7. LET’S START WITH A QUESTION… Q:What happens line 24, when await occurs?
  8. 8. LET’S START WITH A QUESTION… 1
  9. 9. LET’S START WITH A QUESTION… 1
  10. 10. LET’S START WITH A QUESTION… 1
  11. 11. LET’S START WITH A QUESTION… 1
  12. 12. LET’S START WITH A QUESTION… 1
  13. 13. LET’S START WITH A QUESTION… 1
  14. 14. LET’S START WITH A QUESTION… 1
  15. 15. LET’S START WITH A QUESTION… 21
  16. 16. LET’S START WITH A QUESTION… 21
  17. 17. LET’S START WITH A QUESTION… 2 1
  18. 18. LET’S START WITH A QUESTION… 1 2
  19. 19. LET’S START WITH A QUESTION… 1 2
  20. 20. LET’S START WITH A QUESTION… 1 2
  21. 21. LET’S START WITH A QUESTION… 1
  22. 22. LET’S START WITH A QUESTION… 1 2
  23. 23. LET’S START WITH A QUESTION… 2 1
  24. 24. LET’S START WITH A QUESTION… 2 1
  25. 25. LET’S START WITH A QUESTION… 2 1
  26. 26. LET’S START WITH A QUESTION… 2 1
  27. 27. LET’S START WITH A QUESTION… 2 1
  28. 28. LET’S START WITH A QUESTION… 2 11
  29. 29. LET’S START WITH A QUESTION… 1
  30. 30. LET’S START WITH A QUESTION… 1
  31. 31. WAS ITYOUR FIRST ANSWER? Replay?
  32. 32. GUESS WHAT… SAME CODE IN ASP.NET OR UI CONTEXT WILL DEADLOCK!
  33. 33. OH… WAIT! AGENDA 1. Why 2. What 3. How 4. Pitfalls & Recommendations
  34. 34. CHAPTER 1: WHY
  35. 35. LIKE ME, YOU DON’T LIKE WASTE?
  36. 36. A SIMPLE RULE TO AVOID WASTE OF CPU
  37. 37. ASYNC-AWAIT INTENTION IS... …TO EASILYTRANSFORM SYNCHRONOUS CODE  ASYNCHRONOUS CODE (WITHOUT HURTINGYOUR EXISTING CODE STRUCTURE)
  38. 38. WITHOUT HURTINGYOUR EXISTING CODE STRUCTURE
  39. 39. WAIT A MINUTE… ASYNCHRONOUS, CONCURRENT, PARALLEL?
  40. 40. SYNCHRONOUS • PERFORM something here and now. • The caller thread will be blocked until it’s done ASYNCHRONOUS • INITIATE something here and now (off-loading). • The caller thread is released immediately • Free for something else • No waste of CPU resource ;-)
  41. 41. SYNCHRONOUS • PERFORM THIS IS ABOUT INVOCATION! (NOT ABOUT HOWTHE GODDAMNTHING IS EXECUTED) ASYNCHRONOUS • INITIATE
  42. 42. WHAT ABOUT EXECUTION
  43. 43. CONCURRENCY • Multiple “threads” of execution • Independent logical segments
  44. 44. CONCURRENCY PARALLELISM CONCURRENCY + SIMULTANEOUS EXECUTION • Multiple “threads” of execution • Independent logical segments
  45. 45. CONCURRENCY THIS IS ABOUT EXECUTION! (OFTHE GODDAMNTHING ;-) PARALLELISM CONCURRENCY + SIMULTANEOUS EXECUTION • Multiple “threads” of execution • Independent logical segments
  46. 46. CHAPTER 2: WHAT
  47. 47. ASYNC-AWAIT IS NOT ABOUT GOING ASYNC
  48. 48. ASYNC-AWAIT IS ABOUT COMPOSINGTHE ASYNC
  49. 49. COMPOSINGTHE ASYNC WITH TASK CONTINUATIONS
  50. 50. CHAPTER 3: HOW
  51. 51. (PREREQUISITE - TPL)
  52. 52. TASK PARALLEL LIBRARY REMINDER (TPL) • 3 ways to instantiate and run aTASK: • var task =Task.Run(lambda); • var task = newTask(lambda).Start(); • Task.Factory.StartNew(lambda);
  53. 53. TASK PARALLEL LIBRARY REMINDER (TPL) • 3 ways to instantiate and run aTASK: • var task =Task.Run(lambda); • var task = newTask(lambda).Start(); • Task.Factory.StartNew(lambda);
  54. 54. TASK PARALLEL LIBRARY REMINDER (TPL) • 3 ways to instantiate and run aTASK: • var task =Task.Run(lambda); • var task = newTask(lambda).Start(); • Task.Factory.StartNew(lambda); • task.Wait(), task.Result & task.Exception ( all blocking ;-(
  55. 55. TASK PARALLEL LIBRARY REMINDER (TPL) • CONTINUATION • ATask that will be achieve once a previousTask has finished • var continuationTask = previousTask.ContinueWith(lambda); PreviousTask ContinuationTask
  56. 56. ASYNC-AWAIT
  57. 57. ASYNC-AWAIT
  58. 58. ASYNC GENERATES STATE MACHINE
  59. 59. AWAIT MARKS A CONTINUATION
  60. 60. ASYNC
  61. 61. ASYNC GENERATES STATE MACHINE
  62. 62. AT COMPILETIME ASYNC GENERATES STATE MACHINE
  63. 63. AT COMPILETIME ASYNC GENERATES STATE MACHINE
  64. 64. AT COMPILETIME ASYNC GENERATES STATE MACHINE
  65. 65. AWAIT
  66. 66. AWAIT MARKS A CONTINUATION
  67. 67. AWAIT RETURNSTOTHE CALLER WITH A CONTINUATIONTASK
  68. 68. WHO’S DOING THE CONTINUATION? WELL… IT DEPENDS ;-)
  69. 69. IT DEPENDS ON THE AWAITER CONTEXT The following code:
  70. 70. IT DEPENDS ON THE AWAITER CONTEXT The following code: Is equivalent to: WinForms, WPF, ASP.NET…
  71. 71. ASYNC-AWAIT GENERATES STATE MACHINE MARKS A CONTINUATION
  72. 72. THREAD(S) OR NOTHREAD?
  73. 73. WINDOWS I/O: UNDER THE HOOD
  74. 74. WINDOWS I/O: UNDER THE HOOD
  75. 75. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode
  76. 76. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode UI thread THREAD
  77. 77. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode BCL.WriteAsync() THREAD
  78. 78. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode P/Invoke handle THREAD
  79. 79. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Create I/O Request Packet (IRP) (asynchronous) THREAD
  80. 80. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode ReturnTask (await) THREAD
  81. 81. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Release UI thread THREAD
  82. 82. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode NOTHREAD
  83. 83. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Interrupt! NOTHREAD
  84. 84. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Interrupt Service Routine (ISR) NOTHREAD
  85. 85. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Queue Deferred Procedure Call (DPC). NOTHREAD
  86. 86. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Mark IRP as completed NOTHREAD
  87. 87. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Create Asynchronous Procedure Call (APC) to notify the UI process (handle) NOTHREAD
  88. 88. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Execute APC via I/O Completion port (IOCP) thread THREAD
  89. 89. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Execute APC via I/O Completion port (IOCP) thread THREAD
  90. 90. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Post the continuation for UI thread execution THREAD
  91. 91. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode
  92. 92. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Execute continuation (UI thread) THREAD
  93. 93. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Execute continuation (UI thread) THREAD
  94. 94. WINDOWS I/O: UNDER THE HOOD User mode Kernel mode Replay?
  95. 95. CHAPTER 4: PITFALLS & RECOS
  96. 96. #1: DEADLOCK
  97. 97. INITIAL QUESTION, BUT IN A GUI CONTEXT 1
  98. 98. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  99. 99. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  100. 100. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  101. 101. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  102. 102. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  103. 103. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  104. 104. 21 INITIAL QUESTION, BUT IN A GUI CONTEXT
  105. 105. 21 INITIAL QUESTION, BUT IN A GUI CONTEXT
  106. 106. 2 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  107. 107. 1 2 INITIAL QUESTION, BUT IN A GUI CONTEXT
  108. 108. 1 2 INITIAL QUESTION, BUT IN A GUI CONTEXT
  109. 109. 1 2 INITIAL QUESTION, BUT IN A GUI CONTEXT
  110. 110. 1 INITIAL QUESTION, BUT IN A GUI CONTEXT
  111. 111. 1 2 INITIAL QUESTION, BUT IN A GUI CONTEXT
  112. 112. 1 x INITIAL QUESTION, BUT IN A GUI CONTEXT The GUI case
  113. 113. 1 x INITIAL QUESTION, BUT IN A GUI CONTEXT
  114. 114. DEADLOCKS
  115. 115. THE DUPDOB PRINCIPLE “QUI DIT LOCKS… DIT DEADLOCKS” “WHOEVER LOCKS… EVENTUALLY DEADLOCKS” @cyrdup
  116. 116. DEADLOCK MEME
  117. 117. DEADLOCK MEME
  118. 118. DEADLOCK MEME
  119. 119. DO NOT BLOCKTO AVOID DEADLOCK
  120. 120. DO NOT BLOCK TO AVOID DEADLOCK « AWAIT » DON’T BLOCKTHE UITHREAD
  121. 121. YOU CODE A LIBRARY? SYNCHRONIZATION CONTEXT IS NOTYOUR DECISION! AVOID DEADLOCKS IMPROVE PERFORMANCE USE CONFIGUREAWAIT(FALSE) EVERYWHERE!
  122. 122. YOU CODE A LIBRARY? USE CONFIGUREAWAIT(FALSE) EVERYWHERE!
  123. 123. #2: ENTER THE ASYNC VOID
  124. 124. THE ASYNC VOID CASE • Async void is a “fire-and-forget” mechanism... • The caller is unable to know when an async void has finished • The caller is unable to catch exceptions thrown from an async void • (instead they get posted to the UI message-loop)
  125. 125. THE ASYNC VOID CASE • Async void is a “fire-and-forget” mechanism... • The caller is unable to know when an async void has finished • The caller is unable to catch exceptions thrown from an async void • (instead they get posted to the UI message-loop)
  126. 126. TRY-CATCH YOUR EVENT HANDLERS!
  127. 127. MS GUIDELINES • Use async void methods only for top-level event handlers (and their like) • Use asyncTask-returning methods everywhere else • Try-Catch your Async event handlers!
  128. 128. MS EVEN SAID:
  129. 129. #3: USE IT WISELY
  130. 130. ONLY FOR I/O!
  131. 131. WRAP-UP
  132. 132. WRAP-UP 1. Never block! Unless you want to deadlock • Locks, Wait without timeout,Task.Result… • Use top-level await when coding UI or Web • Use ConfigureAwait(false) everywhere within your libraries 2. Never create « async void » methods • And try catch all such existing event handlers 3. Only for I/Os DON’T USE ASYNC-AWAIT UNLESSYOU UNDERSTAND HOW ITWORKS
  133. 133. THANKS!
  134. 134. APPENDIX
  135. 135. DON’T SYSTEMATIZE ASYNC-AWAIT? NOTHING INTHE CONTINUATION? NO NEED FOR AWAIT! (UNLESS FOR ‘USING’) StateMachine StateMachine StateMachine StateMachine StateMachine
  136. 136. ASYNC METHOD GC IMPACT • 3 allocations for every Async method • Heap-allocated state machine • With a field for every local variable in your method • Completion delegate • Task
  137. 137. TROUBLESHOOTING? Namespace public type nested type static method
  138. 138. FEW REFS • Bart De Smet deep dive: https://channel9.msdn.com/Events/TechDays/Techdays-2014-the- Netherlands/Async-programming-deep-dive • Filip Ekberg at Oredev 2016: https://vimeo.com/191077931 • Async-Await Best practices: https://msdn.microsoft.com/en-us/magazine/jj991977.aspx • Compiler error: http://stackoverflow.com/questions/12115168/why-does-this-async-await- code-generate-not-all-code-paths-return-a-value • Task.Run etiquette: http://blog.stephencleary.com/2013/11/taskrun-etiquette-examples- dont-use.html • There is no thread: http://blog.stephencleary.com/2013/11/there-is-no-thread.html • Does usingTasks (TPL) library make an application multithreaded? : http://stackoverflow.com/questions/23833255/does-using-tasks-tpl-library-make-an- application-multithreaded • Eliding Async-Await : http://blog.stephencleary.com/2016/12/eliding-async-await.html

×