Printing in a cell is delayed until the computation of the cell is finished
nasosev opened this issue · 3 comments
Description
Printing in a cell is delayed until the computation of the cell is finished.
This prevents us from live logging statistics of long computations in the notebook.
Expected behavior
Printing should happen incrementally, as in this Python code:
import time
for i in range(10):
time.sleep(1)
print(i)
Actual behavior
Printing in the code below happens simultaneously after 10 seconds:
open System.Threading
for i in 0..9 do
Thread.Sleep 1000
printfn "%A" i
Known workarounds
?
Related information
- Operating system
Win 10 - Branch
IfSharp 3.03 - .NET Runtime, CoreCLR or Mono Version
Core 3.0.100 - Performance information, links to performance testing scripts
NA
Yeah, unlike Python it builds an intermediate buffer then returns it all. If someone would like to change that it'd be fine with me.
At least for me, I prefer a separate channel for reporting which goes to the console rather than the notebook i.e.
open System.Threading
for i in 0..9 do
Thread.Sleep 1000
eprintfn "%i" i
Another alternative is to be more explicit about a desire for asynchrony, an example from the feature notebook that you might simplify down to what you need:
#load "AsyncDisplay.Paket.fsx"
#load "AsyncDisplay.fsx"
open FSharp.Control
let delayedGenerator delay idx =
async {
do! Async.Sleep delay
return idx }
AsyncSeq.initAsync 11L (delayedGenerator 5000) |> Display
AsyncSeq.initAsync 51L (delayedGenerator 1000) |> Display
AsyncSeq.initAsync 501L (delayedGenerator 100) |> Display
Thank you, I was not aware of eprintfn
.
I agree, in general this is actually better: now the notebook is reserved for final results and intermediate ones are shown in the console and do not clutter the notebook.
Thanks for the Async
example, I made a note of it in case I ever need live printing in the notebook.