In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how to use python to obtain the usage rate of cpu per second. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Get the usage requirement of cpu per second
Please obtain the share of cpu for a certain period of time and save it in a persistent form.
Code:
Import psutilimport time# cpu_res = psutil.cpu_percent () # print (cpu_res) # get the share of cpu every second-> persistent storage # how to match the time and the corresponding cpu share to while True: # get the current time and the share of cpu t = time.localtime () cpu_time ='% dub% dvv% d'% (t.tm_hour, t.tm_min) T.tm_sec) cpu_res = psutil.cpu_percent () print (cpu_res) # saved in the file with open ('cpu.txt', 'a') as f: f.write ('% s% s\ n'% (cpu_time, cpu_res)) time.sleep (1)
The 1.psutil module means to obtain the hardware information of the local machine.
2.psutil.cpu_percent means to obtain the share of cpu.
3. With tuple time, you can accurately obtain the hours, minutes and seconds you want.
4. Write the time-> cpu share to the file cpu.txt.
5.time.sleep (1) indicates the sleep time and waits for 1 second before execution.
Output result:
In the cpu.txt file
Python makes full use of cpu
When you use python to deal with large-scale data or run some time-consuming programs, you will often find that the CPU is not full when the program is running, even after multithreading, the CPU utilization is still not high.
This is because by default, Python programs are single processes and are executed using a single CPU core, which means that multi-core processors use only one core.
"one nuclear is difficult, nine nuclear onlookers" means that more than 50% of the computing power will be wasted. This article will introduce how to solve this situation.
Reasons why multithreading cannot increase cpu utilization
The whole process of GIL is Global Interpreter Lock, which means global interpreter lock.
In CPython, the mainstream implementation of the Python language, GIL is a genuine global thread lock, which needs to be acquired when the interpreter interprets and executes any Python code, and will be released when it encounters an Python O operation.
If it is a pure computing program without the Icano operation, the interpreter will release the lock every 100 operations, giving other threads a chance to execute (this number can be adjusted by sys.setcheckinterval).
So although the thread library of CPython directly encapsulates the native threads of the operating system, the CPython process as a whole, only one thread that gets the GIL is running at the same time, and the other threads are waiting for the release of GIL.
This means that even if multithreaded CPU is turned on, the utilization rate will not be very high.
How to make full use of multi-core running program
There are three ways.
1. Multiprocessing
Multiprocessing is a multi-process standard library, which simplifies multi-process python programming to a level similar to multi-threading, and uses multiple CPU cores in a multi-process way.
2. C language extension mechanism
If you don't want to use a heavyweight solution such as multi-process, you can use Cmax Cure + instead. Of course, you don't have to do this either. You just need to write the key parts into Python extensions with C _ Python +, and the rest with Python, so that Python belongs to C of Python,C. Generally, compute-intensive programs are written in C code and integrated into Python scripts (such as NumPy modules) by extension. In this way, you can create native threads in C in the extension, and you can make full use of the computing resources of CPU.
3. Ctypes
However, writing Python extensions is always complicated. Fortunately, Python has another mechanism for interworking with C modules: ctypes. Unlike the Python extension, ctypes allows Python to directly call the export function of any C dynamic library. All you have to do is write some python code in ctypes.
These are all the contents of the article "how to use python to get cpu usage per second". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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
© 2024 shulou.com SLNews company. All rights reserved.