编写程序 控制cpu占用率-3

上一节讲了通过GetTickCount()控制时间片的切换,然而MS .NET FRAMEWORK还提供了PerformanceCounter这一对象,可以获得系统资源的各种性能数据,通过这个PerformanceCounter 对象,我们可以更准确的获得CPU的信息。

上一节连接:

http://blog.csdn.net/weixingstudio/article/details/6867373

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace cpu_3_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            float level=150F;
            PerformanceCounterCategory[] test = PerformanceCounterCategory.GetCategories();
            for (int i = 0; i < test.Length; i++)
            {
                //Console.WriteLine(test[i].CategoryName);
                if (test[i].CategoryName == "Processor")
                {
                    string[] temp = test[i].GetInstanceNames();
                    //Console.WriteLine(temp.Length);
                    for (int j = 0; j < temp.Length; j++)
                    {
                        Console.WriteLine(test[i].MachineName);
                        Console.WriteLine("------------------------");
                        Console.WriteLine(temp[j]);
                        Console.WriteLine("------------------------");

                        PerformanceCounter[] counters = test[i].GetCounters(temp[j]);
                        for (int k = 0; k < counters.Length; k++)
                        {
                            Console.WriteLine(counters[k].CounterName);
                        }
                    }
                }
            }

            Console.WriteLine("*******************************************************");

            PerformanceCounter p = new PerformanceCounter("Processor","% Processor Time","_Total");
            Console.WriteLine(p.NextValue());
            while (true)
            {
                //Console.WriteLine(p.NextValue());
                if (p.NextValue() > level)
                {
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
    }
}


在 Console.WriteLine("*******************************************************"); 语句前的操作暂时不用管,因为我是测试用的。

通过设定一个level值,当CPU的使用率超过这个值以后让其休眠,否则进行不停的空循环。

 

但是结果并不是理想,因为同样像上一节那样出现了一个内核繁忙一个内核空闲的现象,稍后将解决双核处理器的问题。

 

 

在Python中,控制CPU占用率通常不是直接操作的目标,因为Python是一种高级语言,它没有直接控制硬件资源的功能。但是,可以通过编写特定的算法或者使用特定的库来间接地达到控制CPU占用率的效果。 一种常见的方法是通过多线程或者多进程来分散计算任务,从而控制单个程序对CPU的占用。Python标准库中的`threading`模块和`multiprocessing`模块可以用于实现多线程和多进程。在多核处理器上,使用多进程可以较为有效地利用多个CPU核心,因为Python中的全局解释器锁(GIL)会限制同一时刻只有一个线程执行Python字节码,所以多线程在CPU密集型任务中可能不会显著提升性能。 此外,可以使用`psutil`这样的第三方库来监控和控制CPU的使用情况。`psutil`库可以获取当前的CPU使用率,并且可以在某种程度上通过设置进程的CPU亲和性来控制程序运行在特定的CPU核心上。 例如,以下是一个使用`psutil`来限制CPU使用率的基本示例: ```python import psutil import time def limit_cpu_usage(target_cpu_percent): process = psutil.Process() while True: # 获取当前CPU使用率 current_usage = process.cpu_percent(interval=1) # 如果当前使用率超过了目标使用率,则让出CPU if current_usage > target_cpu_percent: time.sleep(0.01) # 设置目标CPU使用率为30% limit_cpu_usage(30) ``` 这段代码通过不断检查当前进程的CPU使用率,并在使用率超过目标值时暂停一小段时间来尝试控制CPU使用率。需要注意的是,这种控制方式并不是十分精确,因为操作系统调度和多任务环境下的其他进程都会影响到实际的CPU使用率
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值