【发布时间】:2017-08-15 05:16:48
【问题描述】:
在运行一些执行并行计算的代码时,输出会出现乱码:不同的消息会混淆。这是一个示例:
Iteration 1
Iteration
Iteration 23 of 19 - Calculating P&L for test window ending at 10/28/1968 12:00:00 AM
of
Iteration 4
Iteration of
Iteration 5
Iteration
Iteration 19 - Calculating P&L for test window ending at of 19 - Calculating P&L for test window ending at 5/29/1974 12:00:00 AM
6 of 878/18/1971 12:00:00 AM19 - Calculating P&L for test window ending at 3/4/1977 12:00:00 AM
of 19 of
of 19 - Calculating P&L for test window ending at 6/25/1985 12:00:00 AM
当顺序运行相同的程序时,控制台输出很好,没有乱码。
打印到控制台是通过这个函数完成的:
let windowTrainTest (comm: Communication) critFoo count (model: IModel) (assets: Assets) (paramList: Parameters list) =
// Deleted some code here
if comm = Verbose then
let msg1 = sprintf "\nwindowTrainTestPandL: First date: %A, Last date: %A\nBest Criterion: %.2f\n" fDate lDate bestCriterion
let msg2 = sprintf "Best Parameters: %A\n" bestParameters
printfn "%s" <| msg1 + msg2
(pandl, wgts), bestParameters, ( ["Criterion", bestCriterion] |> Map.ofList,
["FirstDate", fDate; "LastDate", lDate] |> Map.ofList )
并行化由程序的这一部分完成:
let pSeqMapi f (xs: seq<'T>) = xs |> PSeq.mapi f
let trainTest n i (trainSize, fullSize) =
let takenAssets = assets |> Assets.take (min fullSize len)
lastDate takenAssets
|> printfn "\nIteration %d of %d - Calculating P&L for test window ending at %A\n" (i + 1) n
paramList
|> windowTrainTest comm' critFoo trainSize model takenAssets
let mapTrainTest (initSizes: (int * int) list) =
let f = trainTest initSizes.Length
match calcType with
| PSeq -> initSizes |> pSeqMapi f |> List.ofSeq
| _ -> initSizes |> Seq.mapi f |> List.ofSeq
有没有办法避免这种行为,例如将消息刷新到控制台?
【问题讨论】:
标签: parallel-processing f# console