fsprojects / IfSharp

F# for Jupyter Notebooks
Other
440 stars 71 forks source link

Printing in a cell is delayed until the computation of the cell is finished #237

Closed nasosev closed 4 years ago

nasosev commented 4 years ago

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

cgravill commented 4 years ago

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
nasosev commented 4 years ago

@cgravill

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.

cgravill commented 4 years ago

Great! Glad it works well for you.

On Sat, 5 Oct 2019 at 19:33, Nasos Evangelou-Oost notifications@github.com wrote:

@cgravill https://github.com/cgravill

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.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/fsprojects/IfSharp/issues/237?email_source=notifications&email_token=AAIMPTOGMSIUDUVBLYQMFODQNFFAFA5CNFSM4I4E6YD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOACVI#issuecomment-538706261, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIMPTOMP6CASIFQ42Z4G5LQNFFAFANCNFSM4I4E6YDQ .