1

所以我在书本上进行了一些 CPU 密集型的批量计算。我建立了一个跟踪器来跟踪任务的计算。我关闭了一个邮箱处理器,它在没有并行化的情况下运行良好,但是当我放置一个 array.parallel.map 或异步工作流时,邮箱处理器失败。我想知道为什么?

type timerMessage = 
        | Start of int
        | Tick of bool

let timer = MailboxProcessor.Start(fun mbox ->
let inputloop() = async {
    let progress = ref 0
    let amount = ref 0
    let start = ref System.DateTime.UtcNow
    while true do
        let! msg = mbox.Receive()
        match msg with
        | Start(i) ->   amount := i
                        progress := 0
                        start := System.DateTime.UtcNow
        | Tick(b) -> if !amount = 0 then () 
                        else    
                        progress := !progress + 1
                        let el = System.DateTime.UtcNow - !start
                        let eta = int ((el.TotalSeconds/float !progress)*(float (!amount - !progress)))
                        let etas = (int (eta / 3600)).ToString() + ":" + (int ((eta % 3600) / 60)).ToString() + ":" + (eta % 60).ToString()
                        System.Console.Clear()
                        System.Console.Write((!progress).ToString() + "/" + (!amount).ToString() + " Completed [] Estimated Time Remaining:" + etas)  
} inputloop() )

let computeBook (author :string) path =
    let rs = ReadToStrings(path)
    let bk = StringsToBook rs
    let mt = createMatrix bk 100 10 //size 100 //span 10
    let res = GetResults mt
    //do stuff
    timer.Post(Tick(true))
    (author,path,res)


let partAopA =  //clip head clip foot no word mods
    let lss = seq {for x in processtree do
               for y in (snd x) do
                    yield ((fst x),y) }
    let ls = Seq.toArray lss //task list
    timer.Post(Start(ls.Length)) //start counter
    let compls = Array.map (fun l ->  computeBook (fst l) (snd l) ) ls //Array.Parallel.map fails here the same as below async if I put async blcoks around the computbook call
    //let res = compls |> Async.Parallel |> Async.RunSynchronously
    writeResults compls outputfolder |> ignore
    compls
4

0 回答 0