sloth.timers: Timing functions and classes¶
sloth.timers module contains functions and classes for timing code.
Simple stopwatch for capturing code execution time.
Starts the StopWatch.
Clears the stopwatch and returns the time elapsed since the
start()method has been called. This method will return
0if start has not been called.
The time - in seconds - elapsed since
- Return type
Timer(seconds, func, args=None, kwargs=None):
Simple timer that executes a function after a timed interval
seconds (int) – The number of seconds to call func after.
func (function) – The function to call after seconds seconds have elapsed.
args (list or tuple or None) – Positional arguments to pass to func.
kwargs (dict or None) – Keyword arguments to pass to func.
TypeError – if any of the arguments have incorrect types
Start the timer in the background. Eg:
>>> from sloth.timers import Timer >>> from time import sleep >>> def f(): ... print('Timer finished') ... >>> def a(): ... t = Timer(5, f) ... t.start() ... print('Doing stuff') ... sleep(3) ... print('Doing more stuff') ... sleep(4) ... print('Finished doing stuff while the timer has executed in the background') ... >>> a() Doing stuff Doing more stuff Timer Finished doing stuff while the timer has executed in the background
Cancels the timer. If this method is called before
start(), then the timer will not be run.
Wait until the timer finishes or timeout, if not
None, has elapsed.
timeout (int or float or None) – number of seconds to wait before returning. If None, then it returns when the timer has finished.
Please do not set this attribute unless you know what you are doing.
Controls whether or not the underlying thread that runs the timer is daemonic. This must be set before calling
start(). This value defaults to True, meaning that the timer will be canceled if the program ends before completion.