In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how python sets limits on memory and cpu usage". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Set limits on memory and cpu usage
In linux systems, setting limits on memory and cpu usage using Python is done through the resource module.
Resource document address: resource-Resource usage information
An example of restricting the cpu usage time of a Python process is as follows: import signalimport resourceimport osdef time_exceeded (signo, frame): print ("time's up") raise SystemExit (1) def set_max_runtime (seconds): soft,hard = resource.getrlimit (resource.RLIMIT_CPU) resource.setrlimit (resource.RLIMIT_CPU, (seconds, hard)) signal.signal (signal.SIGXCPU Time_exceeded) if _ _ name__ = ='_ main__': set_max_runtime (5) while True: pass
When you run the above code, a SIGXCPU signal will be generated when the timeout occurs. The program will clean up and quit.
To limit the use of memory, you can use the following function def limit_memory (maxsize): soft, hard = resource.getrlimit (resource.RLIMIT_AS) resource.setrlimit (resource.RLIMIT_AS, (maxsize, hard))
When the memory limit is set, if no more memory is available, the program will start to generate a MemoryError exception.
Note: the above sample code comes from "Python Cookbook" P575 "setting limits for memory and cpu usage".
Query windows cpu, memory usage #-*-coding: UTF-8-*-import osdef get_info (metric): metric_cmd_map = {"cpu_usage_rate": "wmic cpu get loadpercentage", "mem_total": "wmic ComputerSystem get TotalPhysicalMemory" "mem_free": "wmic OS get FreePhysicalMemory"} out = os.popen ("{}" .format (metric_cmd_map.get (metric) value = out.read () .split ("\ n") [2] out.close () return float (value) # cpu utilization cpu_usage_rate = get_info ('cpu_usage_rate') print ("CPU utilization of windows is {}%" .format (cpu_usage_rate)) # unable to find memory usage directly The total memory unit is b, and the remaining memory unit is kbmem_total = get_info ('mem_total') / 1024mem_free = get_info (' mem_free') mem_usage_rate = (1-mem_free/mem_total) * 100print ("windows memory usage is {}%" .format (mem_usage_rate)) "how python sets limits on memory and cpu usage". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.