标签: async-workflow

异步异常处理的意外行为,可能的错误?

在调用恰好为空的嵌套异步时,我偶然发现了一个问题。引发了异常,但无法使用异步工作流提供的任何正常异常处理方法捕获它。

以下是重现问题的简单测试:

[<Test>]
let ``Nested async is null with try-with``() = 

    let g(): Async<unit> = Unchecked.defaultof<Async<unit>>

    let f = async {
            try
                do! g()
            with e ->  
                printf "%A" e
    }

    f |> Async.RunSynchronously |> ignore
Run Code Online (Sandbox Code Playgroud)

这导致以下异常:

System.NullReferenceException : Object reference not set to an instance of an object.
at Microsoft.FSharp.Control.AsyncBuilderImpl.bindA@714.Invoke(AsyncParams`1 args)
at <StartupCode$FSharp-Core>.$Control.loop@413-40(Trampoline this, FSharpFunc`2 action)
at Microsoft.FSharp.Control.Trampoline.ExecuteAction(FSharpFunc`2 firstAction)
at Microsoft.FSharp.Control.TrampolineHolder.Protect(FSharpFunc`2 firstAction)
at Microsoft.FSharp.Control.AsyncBuilderImpl.startAsync(CancellationToken cancellationToken,     FSharpFunc`2 cont, FSharpFunc`2 econt, FSharpFunc`2 ccont, FSharpAsync`1 p)
at Microsoft.FSharp.Control.CancellationTokenOps.starter@1121-1.Invoke(CancellationToken     cancellationToken, FSharpFunc`2 cont, FSharpFunc`2 …
Run Code Online (Sandbox Code Playgroud)

f# asynchronous exception-handling async-workflow

4
推荐指数
1
解决办法
863
查看次数

F#Async.Run与timeout和cancellationToken同步

使用超时和CancellationToken调用Async.RunSynchronously时,似乎忽略超时值.我可以通过在CancellationToken上调用CancelAfter来解决这个问题,但理想情况下我希望能够区分工作流中出现的异常,TimeOutExceptions和OperationCanceledExceptions.

我相信下面的示例代码演示了这一点.

open System
open System.Threading

let work = 
    async {
        let endTime = DateTime.UtcNow.AddMilliseconds(100.0)
        while DateTime.UtcNow < endTime do
            do! Async.Sleep(10)
            Console.WriteLine "working..."
        raise ( Exception "worked for more than 100 millis" )
    }


[<EntryPoint>]
let main argv = 
    try
        Async.RunSynchronously(work, 50)
    with
        | e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)

    let cts = new CancellationTokenSource()

    try
        Async.RunSynchronously(work, 50, cts.Token)
    with
        | e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)  


    cts.CancelAfter(80)
    try
        Async.RunSynchronously(work, 50, cts.Token)
    with …
Run Code Online (Sandbox Code Playgroud)

f# async-workflow cancellation-token

4
推荐指数
1
解决办法
1160
查看次数

让!按顺序执行?

我的印象是让我们!在f#中,智能足以在并行中执行赋值序列.但是,以下示例显示不同的行为,a,b,c的分配似乎同步执行.

    let sleep sec =
        async
            {
                System.Threading.Thread.Sleep(sec * 1000)
                return sec
            }

    let bar = async 
                {
                    let! a = sleep 1
                    let! b = sleep 3
                    let! c = sleep 3
                    return a+b+c
                }

    let foo = Async.RunSynchronously(bar)
    printfn "%d" foo
Run Code Online (Sandbox Code Playgroud)

那是怎么回事?

如果我想在并行执行a,b,c,我应该使用Async.Parallell ... |> Async.RunSynchronously ...然后呢?

上面的示例是无用的,真正的用例就像查询数据库并同时调用一些web服务.

f# async-workflow

2
推荐指数
1
解决办法
246
查看次数

在调用F#异步工作流时,我可以避免定义临时标签吗?

假设我有一个异步数据源:

let getData() = async { return [ 3.14; 2.72 ] }
Run Code Online (Sandbox Code Playgroud)

我可以使用let!和临时标签来调用它:

let showData1() = async {
    let! data = getData()
    data
    |> Seq.iter (printfn "%A")
}
Run Code Online (Sandbox Code Playgroud)

或者,我可以称之为(效率低下!)使用Async.RunSynchronously和管道,没有临时标签:

let showData2() = async {
    getData()
    |> Async.RunSynchronously
    |> Seq.iter (printfn "%A")
}
Run Code Online (Sandbox Code Playgroud)

我喜欢语法,showData2但知道调用Async.RunSynchronously绑定了调用线程.

是否在某处定义了语法,运算符或函数,允许我将其Async<'T>输入到接受的函数中'T?

f# async-workflow

2
推荐指数
1
解决办法
147
查看次数