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

How to meets​ Async and Task

How to meets​ Async and Task

Can I connect by seamless between .NET Tasks and F# Asyncs?​
Before thinking, very nearly structure and interfaces for Task and Async<‘T>.​

Then?​

Can use await in C# for F#’s Async<‘T>?​
And/or can use let! in F# async workflow for Task?​

------

Microsoft MVP Summit Special: A Night of Lightning Talks in SEATTLE

Kouji Matsui

November 06, 2016
Tweet

More Decks by Kouji Matsui

Other Decks in Programming

Transcript

  1. How to meets
    Async and Task
    2016.11.08 MVP SUMMIT SPECIAL: A NIGHT OF LIGHTNING TALKS
    KOUJI MATSUI (@KEKYO2)

    View full-size slide

  2. HELP PLEASE!!
    I’m hearing and speaking VERY HARD,
    So try to making for CODE AND TECH DRIVEN these slides.
    Thanks slow down speaking :)

    View full-size slide

  3. Kouji Matsui - kekyo
    • NAGOYA city, AICHI pref., JP
    • Twitter – @kekyo2 / Facebook
    • ux-spiral corporation
    • Microsoft Most Valuable Professional VS
    and DevTech 2015-
    • Certified Scrum master / Scrum product
    owner
    • Center CLR organizer.
    • .NET/C#/F#/IL/metaprogramming or like…
    • Bike rider

    View full-size slide

  4. Agenda
    Continuation Passing Style
    .NET Task / async-await / F# Async workflow
    Continuation Linking

    View full-size slide

  5. Continuation Passing Style
    Continuation-passing style (CPS) is a style of programming in which
    control is passed explicitly in the form of a continuation.
    “continuation-passing style” - Wikipedia

    View full-size slide

  6. Continuation Passing Style
    Continuation function
    passing to argument
    Continuation implement with
    nested lambda expressions…
    Standard direct style

    View full-size slide

  7. Continuation Passing Style (Details)
    Continuation-Passing style
    Continuation implementation
    nearly equals “Callback”

    View full-size slide

  8. Continuation Passing Style (Details)
    The callback technics likely async continuation handler in JavaScript.
    Continuation-Passing style
    Don’t worry!!
    This technics naming “CPS!!”

    View full-size slide

  9. Continuation Passing Style
    Understand CPS?

    View full-size slide

  10. Agenda
    Continuation Passing Style
    .NET Task / async-await / F# Async workflow
    Continuation Linking

    View full-size slide

  11. The keyword: .NET Task
    The “Task” control interface in .NET 4.0.
    In .NET 4.5 with C# 5.0, Task can handle in “async-await” style.
    What relationship
    between Task and CPS?

    View full-size slide

  12. .NET Task (In .NET 4.0 / C# 4.0)
    .NET 4.0 Task is using with ContinueWith function.
    Continuation passing into Task.ContinueWith()
    --> likely CPS

    View full-size slide

  13. .NET Task (In .NET 4.5/C# 5.0)
    And in .NET 4.5/C# 5.0 with “async-await” style:
    That means
    “Continuation” OK?
    Using Task with async-await

    View full-size slide

  14. .NET Task comparison
    So, the continuation in async-await:
    Means CPS for “async-await” syntax sugar.
    In .NET 4.0 (C# 4.0 Task only) And .NET 4.5 (C# 5.0 async-await)

    View full-size slide

  15. F# Async workflow
    Async workflow supported from F# 2.0.
    Integrated seamless async handling into F# syntax (In .NET 2.0)
    What relationship
    between Async and CPS?

    View full-size slide

  16. F# Async workflow
    Structure nearly equals “async-await.”
    That’s CPS?
    Returned Async<‘T> type
    ※ let! (Let-Bang) is awaiting for Async<‘T>, and bound ‘T value.
    Means C#’s await keyword.
    ※ If continuation implement in F# Asyncworkflow, use Async.FromContinuations<‘T>.

    View full-size slide

  17. Overall Async implementations
    .NET Task : using ContinueWith
    .NET Task : using async-await
    F# Async workflow : using let!
    Both all patterns nearly with Continuation-Passing style?

    View full-size slide

  18. Agenda
    Continuation Passing Style
    .NET Task / async-await / F# Async workflow
    Continuation Linking

    View full-size slide

  19. Connection both continuations
    Can I connect by seamless between .NET Tasks and F# Asyncs?
    ◦ Before thinking, very nearly structure and interfaces for Task and Async<‘T>.
    Then?
    ◦ Can use await in C# for F#’s Async<‘T>?
    ◦ And/or can use let! in F# async workflow for Task?
    ◦ If can, use let!, use!, do!, return! handling directly with HttpClient.GetStreamAsync().

    View full-size slide

  20. Senario for .NET Task / C# side:
    Declare augumented function “AsTask” on Async<‘T> class.
    Can conversion from Async<‘T> to Task.
    ◦ Use Async.StartWithContinuationsfunction:
    Manipulate “TaskCompletionSource class” (Handled SetResult,
    SetException and SetCanceled functions) and returns Task.
    Implements Async<‘T>.GetAwaiter function (Augumented).
    ◦ Can directly awaitable for Async<‘T> in C#.

    View full-size slide

  21. Senario for .NET Task / C# side:
    Can directly awaitable for Async.
    C# compiler invoking GetAwaiter function.

    View full-size slide

  22. Senario for F# Async workflow side:
    Declare augumented function “AsAsync” on Task class.
    Can conversion from Task to Async<‘T>.
    ◦ Use Async.FromContinuations function:
    Manipulate Async<‘T> functions from Task.ContinueWith function’s callbacks.
    And declare augumented function “Source” on AsyncBuilder<‘T> class.
    ◦ Source function argument is “Task” type and convet to Async<‘T>,
    then can use directly let!, use!, do! and return! for Task in F# Async workflow.

    View full-size slide

  23. Senario for F# Async workflow side:
    Can directly awaiting for Task.
    F# compiler translate calling “Source” function.

    View full-size slide

  24. Other tips:
    Conversion both System.Voidand FSharp.Unit in type-safer:
    ◦ If awaiting for Async in C#, cannot receive result value (void).
    ◦ If using Task (Non-generic) in F# async workflow, can only do! expression.
    Support the “CancellationToken”:
    ◦ .NET Task is explicitly handling CancellationToken (ex: argument received).
    F# Async workflow is implicitly handling CancellationToken (Async.DefaultCancellationToken).
    ◦ --> Add CancellationToken argument for AsTask/AsAsyncfunctions.
    Don’t use Async.RunSynchronous function:
    ◦ If use RunSynchronous function, will be hard-blocked current thread nearly invoking Task.Wait.
    Implements using for Async.FromContinuations<‘T>or Async.StartWithContinuations<‘T>.
    Implement new type “AsyncCompletionSource<‘T>”:
    ◦ Async.FromContinuations function handle diffecults (by callback structure) for internal impls.
    I want to delegation-based interface likely “TaskCompletionSource” class.

    View full-size slide

  25. Thanks join!
    The implementations --> GitHub: FusionTasks
    ◦ https://github.com/kekyo/FSharp.Control.FusionTasks
    ◦ https://www.nuget.org/packages/FSharp.Control.FusionTasks.FS40/
    Progressing:
    ◦ FusionTasks (for .NET Core waiting for F# RTM :) Adjusting PCL profiles :(
    ◦ fscx (F# expandable compiler project) https://github.com/fscx-projects/
    ◦ RelaxVersioner (Git based very easy versioner)
    https://github.com/kekyo/RelaxVersioner/
    My blog (Sorry default language in Japanese)
    ◦ http://www.kekyo.net/

    View full-size slide