In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use psutil in Python. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
The psutil module is a cross-platform library https://github.com/giampaolo/psutil
Command line installation via pip:
pip install psutil
If you install Anaconda like me, then this step is left, because it comes with it.
as the name
psutil = process and system utilities
It is specifically used to obtain operating system and hardware-related information, such as CPU, memory, disk, network, process management, etc.
Today's article will introduce you to its common functions and methods of use.
Query CPU information
First import the psutil module to obtain CPU information data.
import psutil# CPU logical count psutil.cpu_count()# CPU physical core psutil.cpu_count(logical=False)#Count CPU user/system/idle time psutil.cpu_times()# interval: Refresh every 0.5s # percpu: View all CPU usage for x in range(5): print(psutil.cpu_percent(interval=0.5, percpu=True))
Output:
Note: If you are interested in Jupyter Notebook outputting multiple variables at the same time, you can check out this article.
15 Jupyter Notebook tips to master (summary)
Query memory information
Output memory usage (total memory, available memory, memory usage, used memory).
psutil.virtual_memory()
For example, the total=16499146752 output above is the total memory 16G, used memory/total memory = 76.0%(memory utilization).
Query disk information
Disk partitions, disk usage, and disk IO information can be obtained through psutil.
#Disk partition information psutil.disk_partitions()#Disk usage psutil.disk_usage ('/')#Disk IOpsutil.disk_io_counters()
Among them, the IO information indicator returned includes disk IO information.
read_count
write_count
read_bytes (IO write bytes)
read_time
write_time (disk write time)
Query network information
Use psutil library to query the number of bytes/packets read and written on the network.
psutil.net_io_counters()
Among them, the returned data indicators include
btes_sent: Number of bytes sent
bytes_recv: Number of bytes received
packets_sent: Amount of packet data sent
packets_recv: Amount of packet data received
errin: Number of errors when receiving packets
errout: Number of errors when sending packets
dropin: Number of dropped packets received
dropout: Number of times a packet is dropped when it is sent
In addition, there are many functions to obtain network interface and network connection information.
such as
psutil.net_if_addrs() Get network interface information
psutil.net_if_stats() Get network interface status, etc.
Query process information
Finally, the psutil module can also be used to obtain detailed information about all processes!
psutil. pies () #All process IDs
The returned result includes all process IDs (pid).
According to pid, you can get the Process object corresponding to a process, and this object contains all the data of the process.
Below we specify process ID=113408, which is actually the current Python interaction environment, to get information about the process.
#Get the specified process ID=113408, which is actually the current Python interactive environment p = psutil.Process(113408)#Process name p.name()#Process exe path p.exe ()#Process working directory p.cwd()#Process start command line p.cmdline()#Current process idp.pid
Thank you for reading! About "how to use psutil in Python" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un