Does Async <T> start returning in F # function from a non-async Func <T> call from C #?

Let's say I want to call this C # function from F #:

public static class Foo {
    public Bar Baz()
    {
       ...
    }
}

The problem is that this function has processor intensity, and I do not want to block it. Unfortunately, the C # library has no overload Task<Bar> BazAsync().

Then I want to provide the asynchronous version myself, by creating an F # function that calls it, and returns (already running) Async<Bar>. That is, I do not want to use System.Threading.Task<Bar>.

I assume that what I am looking for is equivalent Task<T>.Run()in F # Async's way of doing things.

I have already considered the following options:

  • Async.StartAsTask → works with the type of task C # ish.
  • Async.Startand Async.StartImmediatelyAsync<unit>do not receiveAsync<T>

I Async.StartChildwhat am I looking for? If yes, it will be:

let BazWrapper() =
    let asyncTask: Async<Bar> = async {
        return Foo.Bar()
    }
    Async.StartChild asyncTask

However, if it is higher, this solution:

  • F # async StartChild?
  • BazWrapper Async<Async<Bar>> Async<Bar>?
+4
1

BazWrapper Async<Async<Bar>>, , StartChild: Async<'T> Async<Async<'T>>. , async, "" . Async.Start vs Async.StartChild:

async {
    //(...async stuff...)
    for msg in msgs do 
        let! child = asyncSendMsg msg |> Async.StartChild
        ()
    //(...more async stuff...)
}

async, let! "" Async<'Whatever>, 'Whatever. Async.StartChild 'Whatever Async<'T>.

, Async.StartChild, :

let BazWrapper() =
    let asyncTask: Async<Bar> = async {
        return Foo.Bar()
    }
    async {
        let! child = Async.StartChild asyncTask
        return! child
    }

, , , "" (, ), "" async . ( , , .) , , :

let BazWrapper() =
    async {
        return Foo.Bar()
    }

BazWrapper() Async, , Async.RunSynchronously, , async.

+6

Source: https://habr.com/ru/post/1695275/


All Articles