Before you begin, it is crucial to understand that the resource module is a UNIX-specific package that will not work in the POSIX, i.e. Windows Operating System.
While working with system monitoring and resources, we began to question whether there was a better way to monitor system information than manually going through all of the system logs in the control panel.
A bit further away from developing an idea relating to that concept, we get a glimmer of hope that this is possible and entirely possible in the format of a script.
Using python-crontab, sched, and the dateutil module to automate the script would offer an automatic update log scheduled at a specific interval every day, eliminating the need to manually receive information at a specific time.
But, before we try to automate it, we’ll need something to provide you with this information, which is where the resource module comes in.
The resource module is exactly what we’re seeking for because it provides basic information about system resources as well as control functionality.
Using Python’s resource module
Because the resource module is part of Python’s standard library, it does not need to be installed separately. This implies that working with the module on a fresh new server or client with Python installed should be seamless.
However, it has been observed that some versions of Python appear to have problems with the resource module; as a result, it is advised that the resource module be installed using the pip command.
pip install python-resources
Collecting python-resources Downloading python-resources-0.3.tar.gz (8.3 kB) Building wheels for collected packages: python-resources Building wheel for python-resources (setup.py) ... done Created wheel for python-resources: filename=python_resources-0.3-py3-none-any.whl size=7510 sha256=4a4479fd18b 0d1 c2028d460952c65c9abd89fdd6e3b5f5563a039d8b65097af5 Stored in directory: /root/. cache/pip/wheels/12/07/9a/82bc654c835cf0b2c57571b89905d9f4adca8d8b8662676419 Successfully built python-resources Installing collected packages: python- resources Successfully installed python-resources-0.3
How to set up the ecosystem?
Importing the module
# Import all the functions from the resource module using the import keyword from resource import * # Import time using the import keyword import time
The utilization of the underlying parameter
The module’s functionality is primarily determined by the parameters passed to the function that returns the relevant information.
Here are a few instances of these parameters:
- resource.RUSAGE_SELF: Consumed resources by the calling process.
- resource.RUSAGE_CHILDREN: Consumed resources by the children process.
- resource.RUSAGE_BOTH: Resources used by both the current and child processes.
- resource.RUSAGE_THREAD: Consumed resources by the current thread.
To specify which process information was requested, all of these RUSAGE_* symbols are supplied to the getrusage() function.
# Import all the functions from the resource module using the import keyword from resource import * # Import time using the import keyword import time # Apply sleep method for some random n milliseconds say 2. # (Function used to get information about the current process's or its children's # resources consumption t Task that is not CPU bound) time.sleep(2) # Pass RUSAGE_SELF as an argument to the getrusage() function and print it. # (Calling the resources that the current process has consumed) print(getrusage(RUSAGE_SELF)) # Iterate till the 10^8 using the for loop # Inside the for loop, perform some random operation say addition, subtraction etc # (CPU bound task) for i in range(10**8): # Inside the for loop, perform some random operation say addition or # subtraction etc _ = 2+3 # Pass RUSAGE_SELF as an argument to the getrusage() function and print it. # (Calling the resources that the current process has consumed) print(getrusage(RUSAGE_SELF))
resource.struct_rusage(ru_utime=9.211173, ru_stime=0.35864999999999997, ru_maxrss=116308, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=36434, ru_majflt=9, ru_nswap=0, ru_inblock=13656, ru_oublock=8, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=3750, ru_nivcsw=1426) resource.struct_rusage(ru_utime=16.827941, ru_stime=0.37112999999999996, ru_maxrss=116308, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=36441, ru_majflt=9, ru_nswap=0, ru_inblock=13656, ru_oublock=8, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=4998, ru_nivcsw=1836)
Working with this module should have given you a sense of the resources that the resource module can retrieve.
This module could be extended and implemented in a script to monitor system operations and resource consumption at regular intervals.
If you want to work with such an idea, you should check at additional modules like as psutil, sys, and os to work with system processes.
You might want to look at working with the dateutil, sched, and python-crontab modules to schedule the checks to be automatic
The majority of the use cases for this module involve working with the creation of scripts that tend to monitor the system’s functioning and procedures.
If you want to work with system processes, testing, and monitoring, as indicated in the previous section, check out the psutil, sys, os, and dateutil modules.