Timer#
- class cloudly.util.timer.timer[source]#
Bases:
objectThe class
timer(intentionally un-capitalized) can be used as a function decorator or a block context manager. As a function decorator:@timer() def myfunc(...) ...
As a block timer:
with timer(name='block 1'): ... ...
In the first usage, you usually leave name to the default behavior, which plugs in the name of the function that’s being decorated. In the second usage, you may choose to specify a name for the enclosed block; otherwise, there is no printout of “… started” and “… finished” enclosing the execution of the block.
The optional print_func is the builtin function print by default. It may be useful to pass in a logging function, such as print_func=logger.info. However, the line-number info of the log message will be with regard to the current module rather than the context where timer is being used.