timholy / timholy/ProgressMeter.jl

Feature request: support `@showprogress` for function type

Open
#116 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
789
Forks
91
PR merge metrics
No merged PRs in 30d

Description

Currently, @showprogress doesn't work for functions, e.g.,

function foo(n)
    for i in 1:n
        sleep(0.1)
    end
end
@showprogress foo(50) # The ideal usage, doesn't work

Why we need this?

  • In some situations one does need a silent execution, and in some situations one doesn't.
  • Some package authors don't intend to give any additional execution information other than the results

At present, we could manually do something like this... Not complicated, but this requires some document reading, and when the real kernel function is nested deeply, this requires a bunch of modifications on the APIs.

function foo(n; quite = true)
    p = quite ? nothing : Progress(n)
    for i in 1:n
        sleep(0.1)
        quite ? nothing : next!(p)
    end
end

Take the case of this foo, I think the reason @showprogress won't work for this is because we can't directly get the definition of foo when calling it so that unable to parse the expression. (Ref: https://github.com/JuliaLang/julia/issues/2625, https://github.com/JuliaLang/julia/issues/24347)


I'm wondering if the following alternative procedure is practical to support this feature. (please excuse me for my current status of lacking of experiences in macros, I'm just plotting the general idea)

1. use a @progress macro to generate two functions: a wrapper and a kernel

Let

@progress function foo(n)
    for i in 1:n
        sleep(0.1)
    end
end

generate

function foo_(n)
   quote 
        for i in 1:$n
            sleep(0.1)
        end
    end
end
foo(n) = eval(foo_(n))

By doing this, calling foo(n) is still works as usual -- no Progress is involved. But this gives the possibility to parse the function expressions.

2. write a parser to foo_

Let parse(foo_(50)) returns

quote 
    @showprogress for i in 1:50
        sleep(0.1)
    end
end

3. modify @showprogress to support calling of parse(foo_(50))

let @showprogress foo(50) actually calling eval(parse(foo_(50))), so that this feature is supported


As a summary,

the package author can simply add a @progress notation to support the feature

@progress function foo(n)
# some definitions...
end

and the package user uses @showprogress notation to plot progress

@showprogress foo(50)

This's a solution I think is possible. However, since I'm currently not very familiar with macro writing, I'm not very sure of it.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the existing @showprogress macro and the Julia macro behavior discussed in the issue, then evaluate whether the proposed @progress wrapper and parser can support function calls. Done means users can annotate a function with @progress and invoke it through @showprogress while preserving ordinary calls and displaying progress.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.