在运行一些执行并行计算的代码时,输出会出现乱码:不同的消息会混杂在一起。这是一个示例:
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
有没有办法避免这种行为,例如通过将消息刷新到控制台?
最佳答案
并行计算在不同的线程上运行,并且如果一个线程在printfn
的中间中断,而第二个线程在第一个线程再次运行之前运行printfn
,则它们的输出将被交错。
解决此问题的最简单方法是创建一个新函数,该函数将在lock
调用周围使用printfn
关键字:
let lockObj = new obj()
let lockedPrintfn msg = lock lockObj (fun _ -> printfn msg)
然后将所有
printfn
调用替换为lockedPrintfn
,您应该获得期望的序列化输出。由于线程偶尔会花一些时间等待printfn锁,因此性能只会受到一点影响,但是只要您的计算所花费的时间明显长于打印输出所花费的时间,您实际上就不会注意到性能略有降低。