Flux.jl icon indicating copy to clipboard operation
Flux.jl copied to clipboard

Add a macro `@throttle`

Open mcabbott opened this issue 2 years ago • 1 comments
trafficstars

We're discussing removing callbacks in favour of just writing a loop.

One nice thing you can do with callbacks is cb = throttle(mysave, 60) to save roughly once a minute, etc. This PR adds a macro that lets you write @throttle 60 mysave() inside your for-loop. It should accept an arbitrary block of code, which may depend on local variables, e.g.:

for i in 1:100
    g = ...
    @throttle 0.5 begin  # macro creates fun(save, m, g, i) = begin ...
       save("file", m, g, i)
    end
    ...

(It re-uses the function throttle, and has to eval some things to happen before the loop it's in.)

PR Checklist

  • [ ] Tests are added
  • [ ] Entry in NEWS.md
  • [ ] Documentation, if applicable

mcabbott avatar Nov 21 '22 16:11 mcabbott