JuliaLogging / ProgressLogging.jl

MIT License
50 stars 8 forks source link

Incremental progress logging #44

Open pfitzseb opened 3 years ago

pfitzseb commented 3 years ago

Would be nice to support logging progress increments, so that e.g. something like

@withprogress name="threaded iteration" begin
    Threads.@threads for i in 1:12
        sleep(2)
        @logprogress increment = 1/12
    end
end

works.

MasonProtter commented 9 months ago

Here's what I do to make this work:

mutable struct Prog
    N::Int
    @atomic count::Int
end
Prog(N) = Prog(N, 0)
inc!(p::Prog) = @atomic p.count += 1
macro inc(p)
    quote
        local M = $inc!($p) # It seems to be important to separate this out from the `@logprogress` step since `@logprogress` can double-evaluate
    $ProgressLogging.@logprogress M / $p.N
    end |> esc
end

and then

using OhMyThreads: tforeach
p = Prog(N)
@withprogress tforeach(1:N) do i
    sleep(2)
    @inc p
end