asynchronous - F# using Async.Parallel to run 2 tasks in parallel -
assuming have these 2 functions:
let dowork n = async { printfn "work %d" n } let work = async { do! async.sleep(2000) printfn "work finished %d" }
how use async.parallel run them concurrently , wait both finish before proceeding?
async.parallel
takes sequence of async. in case pass list.
[dowork 1; work 2] |> async.parallel |> async.runsynchronously |> ignore
if want return different types of data use discriminated union.
type workresults = | dowork of int | work of float32 let dowork n = async { printfn "work %d" n return dowork(n) } let work = async { do! async.sleep(2000) printfn "work finished %d" return work(float32 / 4.0f) } [dowork 1; work 2] |> async.parallel |> async.runsynchronously |> printf "%a"
output
work 1 work finished 2 [|dowork 1; work 0.5f|]
Comments
Post a Comment