Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the operating system level module Psutil in the Python library

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use the operating system-level module Psutil in the Python library". In the daily operation, I believe that many people have doubts about how to use the operating system-level module Psutil in the Python library. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the operating system-level module Psutil in the Python library". Next, please follow the editor to study!

/ module introduction /

1. Installation. Use the pip command to install.

Pip install psutil

2. Basic usage

As usual, first use dir to see what methods it has. In order not to look at those methods that are rarely used, the editor decided to do this:

3. As you can see, there are not many commands, because some underlined commands have been filtered, but they are enough. There are many methods that we are familiar with. Try a few first and see if the result is the same as we thought.

You can see that the first command prints out the current user name information, and the second command prints out all the pid in the system, which is as follows:

You can see all the process numbers, so what's the use of getting them? The answer is yes. We can use these process numbers to get the process related to the program and then get the directory of the program.

4. So how should we get the process through the process number? We have a process method here, and anyone who knows the process knows that this is a process method, so let's try this one.

Let's first select a pid to see if we can capture the process. We select 684, as shown in the following figure:

5. So we get the process, and then we use the method:

Pp.name ()

You can get which program name is corresponding to the process, and there is still time later.

6. Can we now list all the process names and processes? Please take a look at the following picture:

Is it so simple that you can't imagine? On the contrary, we can also use the process name to figure out all the pid numbers of the process. Oh, please see the following figure:

7. Through the further extension of the process method, it is found that there are many interesting methods:

Pp.children (recursive=True) returns the child elements of this process as a list of processes If recursion is true to return all descendants of the parent pp.cmdline () command line pp.connentions (kind='inet') returns the socket connection opening procedure as a list pp.cpu_affinity (cpus=None) gets or sets the CPU association procedure pp.cpu_percent (interval=None) returns a floating point number that represents the current process's CPU pp.cpu_times () returns a (user, system, Sub-user subsystem) represents the process pp.create_time () process creation time pp.cwd () the absolute path of the current working directory of the process pp.environ () environment variable pp.exe () an absolute path to the executable of the current process pp.kill () kills the current process pp.username () the user owns the name of the process pp.ppid () returns the process parent pid Files opened by the pp.open_files () process pp.resume () restore the impact of all process threads the current state of the pp.status () process the number of threads of the pp.num_threads () process pp.threads () all threads pp.pid process pid pp.terminal () process terminal pp.terminate () process ends

8. There are still some methods that you can explore one by one, and the usage is very simple, so I won't cite any examples here.

9. Let's take a look at psutil's other underlining methods, as shown in the following figure:

You can see that there are many ways to start with cpu,disk,net.

10. Through the accumulation of English at ordinary times, I believe you should be able to guess the usefulness of these methods. Let's take a look.

You can see that the first one is the number of cores of CPU, because I have 4G memory, so the second one is to get the capacity usage of disk c.

11. There is also a very powerful method in psutil, which is test, which can simulate a list like the task manager, as shown in the following figure:

How's it going? isn't that good? Ha, with this knowledge to do the accumulation, I believe you will soon be able to do a software out.

12. Here, the editor makes a decent Mini Program, which prints out the pid, process names and paths of all current processes. The effect is as follows:

You can see that an if statement has been added because some processes are system processes and cannot be accessed without permissions.

At this point, the study on "how to use the operating system-level module Psutil in the Python library" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report