python - 如何操纵python脚本以一定百分比对CPU施加压力?
问题描述
我在网上找到了一个模拟CPU负载的python脚本。问题是它模拟了 100% 的负载。我希望能够根据用户输入设置百分比参数。我怎么能在这里做到这一点?这是代码:
#!/usr/bin/env python
"""
Produces load on all available CPU cores
Updated with suggestion to prevent Zombie processes
Linted for Python 3
Source:
insaner @ https://danielflannery.ie/simulate-cpu-load-with-python/#comment-34130
"""
from multiprocessing import Pool
from multiprocessing import cpu_count
import signal
stop_loop = 0
def exit_chld(x, y):
global stop_loop
stop_loop = 1
def f(x):
global stop_loop
while not stop_loop:
x*x
signal.signal(signal.SIGINT, exit_chld)
if __name__ == '__main__':
processes = cpu_count()
print('-' * 20)
print('Running load on CPU(s)')
print('Utilizing %d cores' % processes)
print('-' * 20)
pool = Pool(processes)
pool.map(f, range(processes))
我还想删除进程以释放使用量。
解决方案
推荐阅读
- amazon-cloudformation - AWS Cloudformation 中的多个自动生成的堆栈
- pdf - Rmarkdown knit pdf - 使用 *italic* 获取带下划线的文本而不是斜体(huxtable 问题?)
- java - 无法调用“javafx.scene.control.MenuButton.getScene()
- r - 多GPU模式不应该更快吗?以玩具为例
- javascript - Discord bot 不响应命令(Javascript)
- apache-spark - 将 spark Java 的最终数据集输出写入 s3
- composer-php - 作曲家需要“结论” - 如何阅读和修复
- ajax - 在 Ajax URL 中传递参数
- powershell - 为什么导出的文件是空白的?
- docker - 新浏览器无法在带有 Docker 的服务器上的 Robot Framework 中运行