When we use Stopwatch in some simple projects, we usually only create one Stopwatch instance.
demo_stopwatch: Stopwatch = Stopwatch()
But in some medium and large projects, we may need to use multiple Stopwatch instances and share them globally. At this point, the Stopwatch instance created directly above may not be easy to share and maintain globally. When multiple Stopwatch instances are required, the management and maintenance of instance objects becomes complicated.
In 0.1.3 or higher, we can use one or more Stopwatch managers to host these Stopwatch instances and share them globally, without the developer having to maintain them.
In the following, we will show you how to use the Stopwatch Manager with a few paragraphs of Python code.
Before using the Stopwatch Manager, we should import the
from stopwatch import StopwatchManager
If you don't need multiple Stopwatch Manager instances, you can use the default Stopwatch Manager directly and share it globally.
from stopwatch import default_manager
default_manager is a variable of data type
StopwatchManager that is created when the package
stopwatch is first imported. Therefore, you can import this variable globally to share the Stopwatch default manager instance globally.
If the Stopwatch default manager is not sufficient to meet your needs, you can create additional Stopwatch manager instances.
demo_manager: StopwatchManager = StopwatchManager(max_stopwatch_count = None)
The constructor method of class
StopwatchManager has an optional parameter
max_stopwatch_count of data type int that indicates how many Stopwatch instances can be hosted by the Stopwatch manager being created at the same time. If this parameter is not supplied or the value is
None, the number of Stopwatch instances is not limited.
At the beginning of this tutorial, we defined a variable called
demo_stopwatch, which is a Stopwatch instance. Now we try to add
demo_stopwatch to the Stopwatch manager instance
demo_manager created above.
demo_manager.add(stopwatch_name = 'guide::demo1',stopwatch_instance = demo_stopwatch)
add has 2 parameters, which are:
The unique name of the Stopwatch instance.
The Stopwatch instance object that was added.
We strongly recommend that you name the Stopwatch instance unique name in the form of a
namespace::name so that when multiple collaborative projects are in the same global scope, the Stopwatch instance unique name conflict is not raised and the Namespace can be the project name.
If the number of Stopwatch instances that are currently hosted in the Stopwatch Manager equals or exceeds the limit of the
max_stopwatch_count parameter, a
MaxLimitError exception is thrown.
In addition to adding an existing Stopwatch instance to the Stopwatch manager, we can also create a new Stopwatch instance with the default constructor method parameters directly in the Stopwatch manager.
new_stopwatch: Stopwatch = demo_manager.create(stopwatch_name = 'guide::demo2')
create has a parameter
stopwatch_name of data type
str, which indicates the unique name of the newly created and added Stopwatch instance. When created and added, this method returns a Stopwatch instance object of data type Stopwatch.
We can also create and add a Stopwatch instance to make it start timing.
new_stopwatch: Stopwatch = demo_manager.create_and_start(stopwatch_name = 'guide::demo2')
We can do this when we need to remove the Stopwatch instance with the specified unique name from the Stopwatch Manager.
demo_manager.remove(stopwatch_name = 'guide::demo2')
If you need to remove all Stopwatch instances, you can use the
When we need to share a Stopwatch instance globally within the project, we don't need to maintain these Stopwatch instances, we use the unique name of the Stopwatch instance to get it from the Stopwatch manager.
demo_stopwatch: Stopwatch = demo_manager.get(stopwatch_name = 'guide::demo1')
If we need to determine if a Stopwatch instance exists, we can check it with the
if not demo_manager.has(stopwatch_name = 'guide::demo2'):print('no such stopwatch')
get_count method can be used when we need to get the number of Stopwatch instances that the specified Stopwatch manager has hosted.
number_of_stopwatchs: int = demo_manager.get_count()
get_count has a return value of the
int data type, which indicates the number of Stopwatch instances.
starts method can be used when we need to start specifying a batch or all of the Stopwatch instances.
demo_manager.starts(stopwatch_names = ['guide::demo1',...])
starts has an optional parameter
stopwatch_names with the data type
list, which indicates a list of unique names for the Stopwatch instance that needs to be started. If this parameter is not supplied or the value is
None, all Stopwatch instances are started.
This method returns a return value of data type int indicating the number of Stopwatch instances that actually started.
Similarly, we can also stop specifying a batch or all of the Stopwatch instances.
demo_manager.stops(stopwatch_names = ['guide::demo1',...])
This method returns a return value of data type
int indicating the number of Stopwatch instances that were actually stopped.
In addition to starting timing and stopping timing, we can also reset a specified batch or all of the Stopwatch instances.
demo_manager.resets(stopwatch_names = ['guide::demo1',...])
get_watchs method can be used when we need to get the current total duration for a specified batch or all of the Stopwatch instances.
total_of_watch: float = demo_manager.get_watchs(stopwatch_names = ['guide::demo1',...],watch_precision = 3)
get_watchs has an optional parameter
watch_precision with an extra data type of
int relative to the other methods mentioned above, which indicates the precision of the total time (the number of decimal places). If this parameter is not supplied or the value is
None, its behavior is consistent with the behavior of the
watch_precision of the Stopwatch instance's
quickstart.py# quickstart.py is python-3.7.4 source fileimport timefrom stopwatch import Stopwatchfrom stopwatch import StopwatchManager# define main functiondef main():# create a stopwatch managerdemo_manager: StopwatchManager = StopwatchManager(max_stopwatch_count = 64)# Start timing after creating and adding a stopwatch instance to the managerdemo_manager.create('guide::demo1').start()# simulated program time-consuming operationtime.sleep(1)# get the stopwatch instance from the manager and stop timing.demo_manager.get('guide::demo1').stop()# total time duration after getting a stopwatch instance from the managerprint(demo_manager.get('guide::demo1').get_watch())# remove the stopwatch instance from the manager.demo_manager.remove('guide::demo1')# define virtual main functionif __name__ == '__main__':main()