Stopwatch Manager
This page will show you how to use the Stopwatch Manager to host multiple Stopwatch instances.
Guide
When we use Stopwatch in some simple projects, we usually only create one Stopwatch instance.
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.
Import module
Before using the Stopwatch Manager, we should import the StopwatchManager
:
If you don't need multiple Stopwatch Manager instances, you can use the default Stopwatch Manager directly and share it globally.
The symbol 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.
Create manager
If the Stopwatch default manager is not sufficient to meet your needs, you can create additional Stopwatch manager instances.
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.
Add Stopwatch
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.
The method add
has 2 parameters, which are:
Parameter
Type
Required
Description
stopwatch_name
str
True
The unique name of the Stopwatch instance.
stopwatch_instance
Stopwatch
True
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.
Note that the Stopwatch instance name must be unique within the current Stopwatch manager scope, otherwise a StopwatchNameError exception will be thrown.
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.
Create Stopwatch
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.
The method 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.
Remove Stopwatch
We can do this when we need to remove the Stopwatch instance with the specified unique name from the Stopwatch Manager.
If you need to remove all Stopwatch instances, you can use the clear
method.
Note that if the specified Stopwatch instance unique name does not exist, a StopwatchNameError exception will be thrown.
Get Stopwatch
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.
If we need to determine if a Stopwatch instance exists, we can check it with the has
method.
Manage Stopwatch
Get the number of instances
The get_count
method can be used when we need to get the number of Stopwatch instances that the specified Stopwatch manager has hosted.
The method get_count
has a return value of the int
data type, which indicates the number of Stopwatch instances.
Make the instance start timing
The starts
method can be used when we need to start specifying a batch or all of the Stopwatch instances.
The method 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.
Note that if one or more instances have started timing, the method will skip instead of raising a StatusError exception.
Stop the instance timing
Similarly, we can also stop specifying a batch or all of the Stopwatch instances.
This method returns a return value of data type int
indicating the number of Stopwatch instances that were actually stopped.
Note that if one or more instances have stopped timing, the method will skip instead of raising a StatusError exception.
Reset the instance
In addition to starting timing and stopping timing, we can also reset a specified batch or all of the Stopwatch instances.
Get the total duration of the instance
The 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.
The method 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 get_watch
method.
Example
Source code
Result
Last updated