0

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

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.....

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

4

1 回答 1

2

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

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

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
于 2012-06-15T01:28:11.683 回答