Skip to content

Instantly share code, notes, and snippets.

@marklap
Last active August 29, 2015 14:15
Show Gist options
  • Save marklap/97bba9ba3652ddeb43a9 to your computer and use it in GitHub Desktop.
Save marklap/97bba9ba3652ddeb43a9 to your computer and use it in GitHub Desktop.
Throttles a function for N number of seconds between calls
################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015 Mark LaPerriere
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
class throttle(object):
"""Throttles a function for N number of seconds between calls
Note: Not compatible with class methods but could be ported to do so
:Parameters:
interval_sec : int | float
The number of seconds to enforce between each call
blocking : bool
Whether or not to insert a sleep between calls otherwise raise an exception
[default: True]
:IVariables:
_init : bool
Determines if this is the first call to the function
interval : float
Stores the number of seconds to enforce between each call
blocking : bool
Block the current call to ensure the interval is kept, otherwise raise an exception
if the time between calls was too short
last_call : float
Keeps track of the last time the wrapped function was called
"""
def __init__(self, interval_sec, blocking=True):
self._init = True
self.blocking = bool(blocking)
self.last_call = time.time()
self.interval = float(interval_sec)
def __call__(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
if self._init:
self._init = False
else:
last_call_delta = time.time() - self.last_call
if last_call_delta < self.interval:
logger.debug(
'Interval {0:f} must be > {1:f}'.format(last_call_delta, self.interval))
if self.blocking:
sleep_sec = self.interval - last_call_delta
logger.debug('Blocking for {0}'.format(sleep_sec))
time.sleep(sleep_sec)
else:
raise Exception(
'Interval {0:f} must be > {1:f}'.format(last_call_delta, self.interval))
result = func(*args, **kwargs)
self.last_call = time.time()
return result
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment