How to wait for async to finish

前端 未结 2 592
挽巷
挽巷 2021-01-13 11:54

I would like to run some async workflow, then wait for it to finish before printing some results, example:

let dowork n =
    async {
        do printfn \"wo         


        
相关标签:
2条回答
  • 2021-01-13 12:05

    I found that it's convenient to use MailboxProcessor to make sure that all side-effects (e.g. printfn) happen on the same thread. Use MailboxProcessor.PostAndReply(singleResult) to funnel work results (be it just the strings to print) into the same synchronization context, and provide for another message type that returns aggregate values. One could use a union for types of these messages.

    0 讨论(0)
  • 2021-01-13 12:15

    Well answering my own question seems lame, but this seems to work. Someone come up with something better so I can award them the answer :)

    let dowork n =
        async {
            do printfn "work %d" n
        }
    
    let creatework() =
        [1..5] |> Seq.map dowork |> Async.Parallel |> Async.RunSynchronously
    
    creatework()    
    printfn "finished"
    

    It gives various output, but "finished" so far is always last...

    0 讨论(0)
提交回复
热议问题