# Flush stdout from workers

**URL:** <https://discourse.julialang.org/t/flush-stdout-from-workers/111669>\
**Category:** General Usage\
**Tags:** distributed\
**Created:** [March 15, 2024, 4:38pm UTC](https://discourse.julialang.org/t/flush-stdout-from-workers/111669 "2024-03-15T16:38:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aal/32/9242_2.png) [@AAL](https://discourse.julialang.org/u/AAL)\
**Post date:** [March 15, 2024, 4:38pm UTC](https://discourse.julialang.org/t/flush-stdout-from-workers/111669/1 "2024-03-15T16:38:48Z")

</div>

I am running simulations in parallel processes that take many hours to run, so I print the number of replications to keep track of the progress. Here is a MWE:

```julia
# test.jl
using Distributed
addprocs(2)

@everywhere function simulate(n)
    for i in 1:n
        sleep(1)
        println(i)
        # flush(stdout)
    end
    return 0
end

function simulate_parallel(n)
    wrkrs = workers()
    nwrkrs = length(wrkrs)
    futures = Vector{Future}(undef,nwrkrs)
    for (i,w) in enumerate(wrkrs)
        futures[i] = @spawnat w simulate(n/nwrkrs)
    end

    results = Vector{Int}(undef,nwrkrs)
    for i in 1:nwrkrs
        results[i] = fetch(futures[i])
    end

    return results
end

@time simulate_parallel(30)

```

So far everything works as expected if I run the file in the REPL or as a script. Sometimes I run a batch of simulations overnight and redirect the output to log files because I want to see how long it took to run each file and if any errors occurred. So I run a batch file that looks like this (I am using Windows):

```julia
julia test.jl > log1.txt 2>&1
julia test.jl > log2.txt 2>&1

```

The `2>&1` means that both stdout and stderr are redirected to the log file.

The problem I have is that the output does not flush to the file (`flush(stdout)` has no effect in my `simulate` function), so I have no idea how far along the simulation is for hours. Is there anyway to force flush every time a workers prints?
