Skip to content

Commit

Permalink
Discard changes to src/evaluation/Throttled.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Aug 12, 2024
1 parent e34fdf2 commit 6b35239
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/evaluation/Throttled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct ThrottledFunction
iscoolnow::Ref{Bool}
run_later::Ref{Bool}
last_runtime::Ref{Float64}
run_threaded::Bool
end

"Run the function now"
Expand All @@ -23,24 +22,13 @@ function Base.flush(tf::ThrottledFunction)
end
end

function _trigger(tf::ThrottledFunction)
if tf.run_threaded
Threads.@spawn begin
flush(tf)
schedule(tf)
end
else
flush(tf)
schedule(tf)
end
end

"Start the cooldown period. If at the end, a run_later[] is set, then we run the function and schedule the next cooldown period."
function schedule(tf::ThrottledFunction)
# if the last runtime was quite long, increase the sleep period to match.
Timer(tf.timeout + tf.last_runtime[] * tf.runtime_multiplier) do _t
if tf.run_later[]
_trigger(tf)
flush(tf)
schedule(tf)
else
tf.iscoolnow[] = true
end
Expand All @@ -50,7 +38,8 @@ end
function (tf::ThrottledFunction)()
if tf.iscoolnow[]
tf.iscoolnow[] = false
_trigger(tf)
flush(tf)
schedule(tf)
else
tf.run_later[] = true
end
Expand All @@ -74,13 +63,13 @@ This throttle is 'leading' and has some other properties that are specifically d
Inspired by FluxML
See: https://github.com/FluxML/Flux.jl/blob/8afedcd6723112ff611555e350a8c84f4e1ad686/src/utils.jl#L662
"""
function throttled(f::Function, timeout::Real; runtime_multiplier::Float64=0.0, run_threaded::Bool=false)
function throttled(f::Function, timeout::Real; runtime_multiplier::Float64=0.0)
tlock = ReentrantLock()
iscoolnow = Ref(false)
run_later = Ref(false)
last_runtime = Ref(0.0)

tf = ThrottledFunction(f, timeout, runtime_multiplier, tlock, iscoolnow, run_later, last_runtime, run_threaded)
tf = ThrottledFunction(f, timeout, runtime_multiplier, tlock, iscoolnow, run_later, last_runtime)

# we initialize hot, and start the cooldown period immediately
schedule(tf)
Expand Down

0 comments on commit 6b35239

Please sign in to comment.