Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous Programming: Scala.concurrent and Monix! What?

Asynchronous Programming: Scala.concurrent and Monix! What?

In the context of executing a task, a synchronous execution indicates that the program waits for the result of the first task before moving on to another task. In contrast, an asynchronous execution does not wait for the result of the first task and it starts executing another task immediately after the first task has begun. Both Futures & Promises, and Monix are Scala libraries that allow users to efficiently perform asynchronous operations.

This talk introduces the Futures & Promises library in Scala.concurrent and Monix. We will walk through several examples that demonstrate how to use Futures & Promises, and Monix. In addition, the talk compares and contrasts the similarities and differences between the two libraries. Furthermore, we will discuss some best practices in debugging asynchronous systems.

Resources on Pg. 41:
Iulian Dragos - Async Debugger in Scala IDE:
http://iulidragos.com/assets/papers/stack-retention.pdf
http://scala-ide.org/docs/current-user-doc/features/async-debugger/index.html
https://github.com/jamie-allen/async_debugger_demo
https://www.youtube.com/watch?v=P32Drw0CX08
IntelliJ Capture (2017):
https://blog.jetbrains.com/idea/2017/02/intellij-idea-2017-1-eap-extends-debugger-with-async-stacktraces/
https://blog.jetbrains.com/idea/2017/02/intellij-idea-2017-1-eap-extends-debugger-with-async-stacktraces/#comment-403133
https://www.jetbrains.com/help/idea/async-stacktraces.html

Yifan Xing

April 18, 2018
Tweet

More Decks by Yifan Xing

Other Decks in Programming

Transcript

  1. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    YIFAN XING - 2018
    ASYNCHRONOUS
    PROGRAMMING
    Yifan Xing
    SCALA.CONCURRENT AND
    MONIX!
    @yifan_xing_e

    View Slide

  2. 01 Synchronous vs. Asynchronous
    Differences
    Futures & Promises
    Debugging Async Programs
    Monix
    A TOUR OF ASYNCHRONOUS
    PROGRAMMING IN SCALA
    02
    03
    04
    05
    Overview
    ASYNCHRONOUS
    PROGRAMMING:
    SCALA.CONCURRENT
    AND MONIX!
    1

    View Slide

  3. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Concurrency
    Synchronous
    Asynchronous
    Single-threaded
    Multi-threaded
    YIFAN XING - 2018 @yifan_xing_e
    2

    View Slide

  4. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Synchronous: Single-threaded
    YIFAN XING - 2018
    Task 1 Task 2 Task 3
    @yifan_xing_e
    3

    View Slide

  5. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Synchronous: Multi-threaded
    YIFAN XING - 2018
    Task 1
    Task 2
    Task 3
    @yifan_xing_e
    4

    View Slide

  6. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Asynchronous: Single-threaded
    YIFAN XING - 2018
    Task 1
    Task 2
    Task 3
    @yifan_xing_e
    5

    View Slide

  7. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Asynchronous: Multi-threaded
    YIFAN XING - 2018
    Task 1
    Task 2
    Task 3
    @yifan_xing_e
    6

    View Slide

  8. Future
    Promise
    ExecutionContext
    Examples
    Scala.concurrent

    View Slide

  9. YIFAN XING - 2018
    Scala.concurrent: Future
    Computation Incomplete:
    Computation Completed:
    Future is not completed
    Future is completed: with a Value => Success
    Future is completed: with an Exception => Failure
    An object holding a value which may become available at some point
    Immutable
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Future
    7

    View Slide

  10. YIFAN XING - 2018
    Example: Callbacks
    Multiple
    callbacks may
    be registered;
    NO guarantee
    that they will be
    executed in a
    particular order.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    8

    View Slide

  11. YIFAN XING - 2018
    Example: Callbacks
    Multiple
    callbacks may
    be registered;
    NO guarantee
    that they will be
    executed in a
    particular order.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    8

    View Slide

  12. YIFAN XING - 2018
    Example: Callbacks
    Multiple
    callbacks may
    be registered;
    NO guarantee
    that they will be
    executed in a
    particular order.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    8

    View Slide

  13. YIFAN XING - 2018
    Example: Callbacks
    Multiple
    callbacks may
    be registered;
    NO guarantee
    that they will be
    executed in a
    particular order.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    8

    View Slide

  14. YIFAN XING - 2018
    Example: Callbacks
    Chain callbacks
    Allow one to enforce
    callbacks to be
    executed in a
    specified order.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    9

    View Slide

  15. YIFAN XING - 2018
    Example: Callbacks
    Chain callbacks
    Allow one to enforce
    callbacks to be
    executed in a
    specified order.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    9

    View Slide

  16. YIFAN XING - 2018
    Example: Callbacks
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Exception in
    callback:
    NOT propagated
    to the
    subsequent
    andThen
    callbacks.
    10

    View Slide

  17. YIFAN XING - 2018
    Example: Transformations
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    11

    View Slide

  18. YIFAN XING - 2018
    Example: Transformations
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    11

    View Slide

  19. YIFAN XING - 2018
    Example: Transformations
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    12

    View Slide

  20. YIFAN XING - 2018
    Example: Transformations
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    13

    View Slide

  21. YIFAN XING - 2018
    Example: Transformations
    More generic &
    flexible
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    14

    View Slide

  22. YIFAN XING - 2018
    Example: Transformations
    More generic &
    flexible
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    14

    View Slide

  23. YIFAN XING - 2018
    Example: Transformations
    More generic &
    flexible
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    14

    View Slide

  24. YIFAN XING - 2018
    Example: Transformations
    More generic &
    flexible
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    14

    View Slide

  25. YIFAN XING - 2018
    Example: Transformations
    More generic &
    flexible
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    14

    View Slide

  26. YIFAN XING - 2018
    Example: Transformations
    If no match
    cases or original future
    contains a valid result:
    New future will contain
    the same result as the
    original one.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Evaluated only after
    a Failure
    15

    View Slide

  27. YIFAN XING - 2018
    Example: Transformations
    If no match
    cases or original future
    contains a valid result:
    New future will contain
    the same result as the
    original one.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Evaluated only after
    a Failure
    15

    View Slide

  28. YIFAN XING - 2018
    Example: Transformations
    If no match
    cases or original future
    contains a valid result:
    New future will contain
    the same result as the
    original one.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Evaluated only after
    a Failure
    15

    View Slide

  29. YIFAN XING - 2018
    Example: Transformations
    If no match
    cases or original future
    contains a valid result:
    New future will contain
    the same result as the
    original one.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Evaluated only after
    a Failure
    15

    View Slide

  30. YIFAN XING - 2018
    Example: Transformations
    If both futures failed,
    the resulting future
    holds the throwable
    object of the first
    future.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    16

    View Slide

  31. YIFAN XING - 2018
    Example: Transformations
    If both futures failed,
    the resulting future
    holds the throwable
    object of the first
    future.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    16

    View Slide

  32. YIFAN XING - 2018
    Example: Transformations
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    16
    If both futures failed,
    the resulting future
    holds the throwable
    object of the first
    future.

    View Slide

  33. YIFAN XING - 2018
    Scala.concurrent: Promises
    A writable, single-assignment container
    Can create a Future
    Can complete a Future, only ONCE
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    Promise:
    17

    View Slide

  34. YIFAN XING - 2018
    Example: Promises
    A Promise can be
    completed at most ONCE.
    If the promise has already
    been fulfilled, failed or
    has timed out, calling this
    method will throw an
    IllegalStateException.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    18

    View Slide

  35. YIFAN XING - 2018
    Example: Promises
    A Promise can be
    completed at most ONCE.
    If the promise has already
    been fulfilled, failed or
    has timed out, calling this
    method will throw an
    IllegalStateException.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    18

    View Slide

  36. YIFAN XING - 2018
    Example: Promises
    A Promise can be
    completed at most ONCE.
    If the promise has already
    been fulfilled, failed or
    has timed out, calling this
    method will throw an
    IllegalStateException.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    18

    View Slide

  37. YIFAN XING - 2018
    Example: Promises
    If the promise has
    already been
    completed returns
    false, or true
    otherwise.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    19

    View Slide

  38. YIFAN XING - 2018
    Example: Promises
    If the promise has
    already been
    completed returns
    false, or true
    otherwise.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    19

    View Slide

  39. YIFAN XING - 2018
    Example: Promises
    If the promise has
    already been
    completed returns
    false, or true
    otherwise.
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    @yifan_xing_e
    19

    View Slide

  40. YIFAN XING - 2018
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Building A Client-facing Interface
    @yifan_xing_e
    20

    View Slide

  41. YIFAN XING - 2018
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    p.future
    Problem in A Client-facing Interface
    @yifan_xing_e
    21

    View Slide

  42. YIFAN XING - 2018
    Monix
    @yifan_xing_e

    View Slide

  43. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    What is Monix?
    Asynchronous, event-based programs
    Data type: Task, etc.
    "Factory" of Future instances
    Lazy & asynchronous computations
    YIFAN XING - 2018 @yifan_xing_e
    22

    View Slide

  44. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Task
    YIFAN XING - 2018
    Lazily evaluated
    Evaluated when
    call runAsync
    Trigger side effects
    @yifan_xing_e
    23

    View Slide

  45. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Task
    YIFAN XING - 2018
    Lazily evaluated
    Evaluated when
    call runAsync
    Trigger side effects
    @yifan_xing_e
    23

    View Slide

  46. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Task
    YIFAN XING - 2018
    Lazily evaluated
    Evaluated when
    call runAsync
    Trigger side effects
    @yifan_xing_e
    23

    View Slide

  47. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Task Callback
    YIFAN XING - 2018
    Eagerly evaluate task
    Register callbacks
    Similar to
    Future#onComplete
    @yifan_xing_e
    24

    View Slide

  48. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Task Callback
    YIFAN XING - 2018
    Eagerly evaluate task
    Register callbacks
    Similar to
    Future#onComplete
    @yifan_xing_e
    24

    View Slide

  49. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Task Callback
    YIFAN XING - 2018
    Eagerly evaluate task
    Register callbacks
    Similar to
    Future#onComplete
    @yifan_xing_e
    24

    View Slide

  50. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Eager/ Lazy
    YIFAN XING - 2018
    Evaluate eagerly
    Equivalent to Future#successful
    Defer eager evaluation
    Similar to Task#eval
    @yifan_xing_e
    25

    View Slide

  51. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Eager/ Lazy
    YIFAN XING - 2018
    Evaluate eagerly
    Equivalent to Future#successful
    Defer eager evaluation
    Similar to Task#eval
    @yifan_xing_e
    25

    View Slide

  52. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Memoization
    YIFAN XING - 2018
    Evaluated lazily
    Not memoized, recompute each
    time the Task is executed.
    Evaluate lazily
    Evaluated exactly ONCE
    Result is memoized
    @yifan_xing_e
    26

    View Slide

  53. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Memoization
    YIFAN XING - 2018
    Evaluated lazily
    Not memoized, recompute each
    time the Task is executed.
    Evaluate lazily
    Evaluated exactly ONCE
    Result is memoized
    @yifan_xing_e
    26

    View Slide

  54. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: MemoizeOnSuccess
    YIFAN XING - 2018
    Exceptions are not cached.
    Only cache the first
    successful result
    @yifan_xing_e
    27

    View Slide

  55. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: MemoizeOnSuccess
    YIFAN XING - 2018
    Exceptions are not cached.
    Only cache the first
    successful result
    @yifan_xing_e
    27

    View Slide

  56. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: MemoizeOnSuccess
    YIFAN XING - 2018
    Exceptions are not cached.
    Only cache the first
    successful result
    @yifan_xing_e
    27

    View Slide

  57. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Alternative Thread Pool
    YIFAN XING - 2018
    Overrides default scheduler
    Overriding the "default"
    scheduler can only happen
    ONCE
    Task is immutable
    @yifan_xing_e
    28

    View Slide

  58. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Alternative Thread Pool
    YIFAN XING - 2018
    Overrides default scheduler
    Overriding the "default"
    scheduler can only happen
    ONCE
    Task is immutable
    @yifan_xing_e
    28

    View Slide

  59. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: Alternative Thread Pool
    YIFAN XING - 2018
    Overrides default scheduler
    Overriding the "default"
    scheduler can only happen
    ONCE
    Task is immutable
    @yifan_xing_e
    28

    View Slide

  60. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Monix: CancelableFuture
    YIFAN XING - 2018
    Calling it multiple
    times has the same side-
    effect as calling it only once.
    @yifan_xing_e
    29

    View Slide

  61. - A value
    - Executed when constructed
    - Eager evaluation
    - ExecutionContext needed whenever use
    operators
    A function -
    Execution controllable by user -
    Lazy evaluation -
    ExecutionScheduler needed whenever -
    use runAsync
    Monix
    Future
    Eager/Lazy
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    30

    View Slide

  62. - Memoized by default
    - Evaluated once
    - Store result
    Explicitly use memoization operators -
    Evaluated whenever needed -
    Referential transparency -
    Monix
    Future
    Memoization
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    31

    View Slide

  63. - Sometimes errors are
    not propagated
    - Override reportFailure in provided
    ExecutionContext to control output
    of unpropagated exceptions
    Scheduler.reportFailure -
    Defaults to System.err -
    Allow customized logging tools -
    Monix
    Future
    ErrorHandling
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    32

    View Slide

  64. Future Monix
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    33

    View Slide

  65. Future
    Monix
    A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    34

    View Slide

  66. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Debugging
    BEST PRACTICES
    In Asynchronous Programs
    @yifan_xing_e

    View Slide

  67. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    35

    View Slide

  68. Writing Async Code
    1. Rethink before
    adding more code
    2. Adding more code to
    fix problems will
    usually add more bugs
    D e b u g
    1. Test code in isolation
    2. Observe what is
    happening
    3. Confirm behavior
    before moving on
    T e s t
    36

    View Slide

  69. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Using Await & Print
    YIFAN XING - 2018 @yifan_xing_e
    TimeoutException if after
    waiting for the specified
    time awaitable is still not
    ready
    atMost may be:
    A finite positive duration
    Negative (no waiting is
    done)
    Duration.Inf for
    unbounded waiting
    37

    View Slide

  70. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Using Await & Print
    YIFAN XING - 2018 @yifan_xing_e
    TimeoutException if after
    waiting for the specified
    time awaitable is still not
    ready
    atMost may be:
    A finite positive duration
    Negative (no waiting is
    done)
    Duration.Inf for
    unbounded waiting
    37

    View Slide

  71. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    ScalaTest: ScalaFutures
    YIFAN XING - 2018 @yifan_xing_e
    Default timeout
    150ms
    TestFailedException
    if not finished after
    timeout
    38

    View Slide

  72. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    ScalaTest: ScalaFutures
    YIFAN XING - 2018 @yifan_xing_e
    Default timeout
    150ms
    TestFailedException
    if not finished after
    timeout
    38

    View Slide

  73. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    ScalaTest: ScalaFutures
    YIFAN XING - 2018 @yifan_xing_e
    Default timeout
    150ms
    TestFailedException
    if not finished after
    timeout
    38

    View Slide

  74. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Resources: Async Debugger & Capture
    YIFAN XING - 2018 @yifan_xing_e
    IntelliJ Capture (2017):
    Iulian Dragos - Async Debugger in Scala IDE
    IntelliJ IDEA 2017.1 EAP Extends Debugger with Async Stacktraces
    Developer's Explanation
    Debugging Tutorial
    Async Stacktraces
    Stack Retention
    Scala IDE Doc
    Demo: Jamie Allen
    Rethink the Debugger: Scala Days 2014
    39

    View Slide

  75. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    Using Async Debugger / Capture
    YIFAN XING - 2018 @yifan_xing_e
    Substituting its parts related
    to the asynchronous code
    execution with where it was
    called
    Sender: Async code executor
    Receiver: executed code
    Config exact signatures of
    methods of the async code
    Stores the stack and
    variables info in a map
    (1000 stacks), where the
    key is a parameter with
    the specified number.
    40

    View Slide

  76. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    YIFAN XING - 2018 @yifan_xing_e
    ( Sorry Alex :P )
    41

    View Slide

  77. A TOUR OF ASYNCHRONOUS PROGRAMMING IN SCALA
    YIFAN XING - 2018 @yifan_xing_e
    42

    View Slide

  78. T H A N K Y O U
    @yifan_xing_e

    View Slide