Why does a function wait for a loop to finish before printing something inside the loop?

I have been using Julia since 0.4 and this is the first time that something like that happens. I have a function with a loop inside it and a print inside the loop. Usually I use this loop to know which parameters make the slower part of the calculation, but now, I do not get any output before the function finishes. This is with Julia 1.4. The function would be something like this:

function f(x)
for r in rr
print(r, " ")
return aux

Probably because print is buffered. Try putting a flush(STDOUT) after the print.

1 Like

Okey, then my question becomes, why is


buffered now? In the other Julias this was not the case.

Printing was always and is asynchronous in julia. It may be caused by many things that, in new julia version, somecomplicatedfun® can run without task switch.

1 Like

Crossref to answer on Stackoverflow to avoid double work in answering: https://stackoverflow.com/questions/60982177/julia-waits-for-function-to-finish-before-printing-message-in-a-loop

1 Like

It would be nice if print / println had a keyword argument option to automatically flush, like for instance in Python

print(..., flush=True)
1 Like

I can also imagine a token that would flush, eg

struct Flush end
Base.print(io::IO, ::Flush) = flush(io)
const FLUSH = Flush()
print(..., FLUSH, other_stuff..., FLUSH)