使用Async.FromContinuations在f#中定制异步原语函数

时间:2012-06-15 00:19:24

标签: asynchronous f#

我对异步工作流程有点新手,我认为它有一些我不太了解的东西......我正在关注真实世界的函数式编程,并以这种方式编写异步原语

let Sleep(time) =
  Async.FromContinuations(fun (cont, econt, ccont) ->
     let tmr = new System.Timers.Timer(time, AutoReset = false)
     tmr.Elapsed.Add(fun _ -> cont())
    tmr.Start()
  );;

我尝试使用非常慢的因子实现编写自己的异步原语,代码是:

let rec factorial(n : int) (mem : bigint) =
   match n with
   | 0 | 1 -> printfn "%A" mem
   | _ ->  factorial (n - 1) (mem * bigint(n))


let BigFactorial(numero,mesaje)=
    Async.FromContinuations(fun (cont,error,cancelation) ->
                                printfn "begin number: %s" mesaje
                                factorial numero 1I |>ignore
                                printfn "End number: %s ." mesaje
                                cont())

现在我写了这个

 Async.RunSynchronously(async{
     printfn "Start!!..."
     do! BigFactorial(30000,"30M")
     do! BigFactorial(10000, "10M")
     printfn "End!!..."
 })

但它并没有精确地以异步方式运行....

Start!!...
begin number: 30M
2759537246219...(A nice long number!)
end number: 30M .
begin number: 10M .
2846259680917054518906...(other nice number)
End!!...

如果我并行执行它,那么效果很好......

let task1 = async{
   printfn "begin"
   do! BigFactorial(30000,"30")
   printfn "end..." 
}

let task2 = async{
   printfn "begin"
   do! BigFactorial(10000,"10")
   printfn "end!!..." 
}


Async.RunSynchronously(Async.Parallel[task1;task2])

begin
begin: 30 
begin
begin: 10M

//long numbers here

end....
end.....

但我希望知道原因(或几个原因!)为什么第一个代码不起作用... 非常感谢,我感谢任何帮助...

1 个答案:

答案 0 :(得分:2)

所有 async绑定一样,do!在当前线程上运行,因此您的两个连续do!按顺序运行。

如果您希望它们并行运行,您必须明确说明:

async {
   printfn "Start!!..."
   let! tokenA = BigFactorial (30000, "30M") |> Async.StartChild // on new thread
   let! tokenB = BigFactorial (10000, "10M") |> Async.StartChild // on new thread
   do! tokenA // block until `BigFactorial (30000, "30M")` finishes
   do! tokenB // block until `BigFactorial (10000, "10M")` finishes
   printfn "End!!..." }
|> Async.RunSynchronously