Context Management#
How to Automatically Start and Stop the Timer#
Instead of manually starting and stopping the Timer with timer.start()
and timer.stop()
, it's recommend to use the context manager and with Timer():
statements.
Advantages#
Apart from less code and more readable code, the built-in context manager automatically closes the Timer (so you don't forget it) when the task is done or if an error occurs.
As an added benefit, the with
statement prevents you from having loose ends lingering in the Python runtime.
Examples#
With Context Manager#
It's recommended to do this:
Without Context Manager#
And not recommended to do this:
Python | |
---|---|
Multiple Threads and Decimals#
It's possible nest multiple instances of context. Simply remember to add a unique thread
argument to each instance of Timer()
.
Python | |
---|---|
Elapsed time: 0.12345 seconds for thread B
Elapsed time: 0.123 seconds for thread C
Elapsed time: 1.23 seconds for thread A
Learn more about decimals and threads.
Singleton and Unique Threads
The Timer()
class is a singleton, which means that there can only be one instance of the class. This is to ensure that the same Timer()
is used for all threads and that each thread is unique.