It’s the buffering of the external program’s output!
Since Julia doesn’t call the external program via a terminal, it’s output is block-buffered: it will only be writen to stdout once there is enough data to be worth the trouble.
In the end, it’s sufficient to get the IO of the external program to be line-buffered (or not buffered at all, but it would be less eficient). That is, to have the output of the program to be effectively written to it’s out-stream (proc.out
) after every line break (\n
). There are multiple ways to instruct the system to do this. I’ve achieved it via the stdbuf
command:
julia> proc = open(`stdbuf -oL ./exec`, write=true, read=true)
Process(`stdbuf -oL ./exec`, ProcessRunning)
julia> println(proc, "12")
julia> readline(proc)
"13"
julia> println(proc, 23)
julia> readline(proc)
"24"
(Notice that all of this means that adding an fflush(stdout)
to the C code in the question would fix the issue. But, in pratice, this wasn’t an option for me since cannot modify the external program.)