• python指定cpu使用率,与内存占用率


    python指定cpu使用率,与内存占用率

    """
    runing.py -c 2 -t 0.01 -m 1000
    -c cpu核数,不加-c参数为最大核数
    -t cpu运算频率时间,间隔,越小占用越高
    -m 内存占用,1000MB
    """
    import sys
    import time
    from time import clock
    import argparse
    from multiprocessing import Process
    from multiprocessing import cpu_count
    import math
    # import tempfile
    
    # # 屏蔽 Traceback信息
    # sys.stderr=tempfile.TemporaryFile() 
    
    
    def exec_func(bt):
    
        while True: 
            for i in range(0, 9600000):
                pass
            time.sleep(bt)
    
    
    
    if __name__ == "__main__":
    
        parse = argparse.ArgumentParser(description='runing')
    
        parse.add_argument(
            "-c",
            "--count",
            default= cpu_count(),
            help='cpu count'
            )
    
        parse.add_argument(
            "-t",
            "--time",
            default= 0.01,
            help='cpu time'
            )
        parse.add_argument(
            "-m",
            "--memory",
            default= 1000,
            help='memory'
            )
    
    
        args = parse.parse_args()
    
        cpu_logical_count = int(args.count)
    
        cpu_sleep_time = args.time
    
        memory_used_mb = int(args.memory)
    
        try:
            cpu_sleep_time = int(args.time)
        except ValueError:
            try:
                cpu_sleep_time = float(args.time)
            except ValueError as ex:
                raise ValueError(ex)
    
        _doc = """
                runing.py -c 2 -t 0.01 -m 1000
                -c 指定cpu核数,不加-c参数默认为当前cpu最大核数
                -t cpu运算频率时间,间隔,越小占用越高
                -m 内存占用,1000MB
    
                CPU使用率需要手动增加减少-t参数来达到,预期使用率。
               """
        print("
    ====================使用说明=========================")
        print("{}".format(_doc))
        print("
    ====================================================")
        print('
    当前占用CPU核数:{}'.format(cpu_logical_count))
        print('
    内存预计占用:{}MB'.format(memory_used_mb))
        print('
    资源浪费中......')
    
        try:
            # 内存占用
            s = ' ' * (memory_used_mb * 1024 * 1024)
        except MemoryError:
            print("剩余内存不足,内存有溢出......")
    
        try:
    
            p = Process(target=exec_func, args=("bt",))
    
            ps_list = []
    
            for i in range(0, cpu_logical_count):
                ps_list.append(Process(target=exec_func, args=(cpu_sleep_time,)))
    
            for p in ps_list:
                p.start()
    
            for p in ps_list:
                p.join()
    
        except KeyboardInterrupt:
            print("资源浪费结束!")

    新测可用:

     结果:

    命令行模式,通过ctrl+c来结束

  • 相关阅读:
    2016四川省赛 Floyd-Warshall
    CF374 Maxim and Array
    CF374 Journey
    hdu5730 Shell Necklace
    hihocoder1388 Periodic Signal
    hihocoder1391 Country
    hdu 5903 Square Distance
    hdu5904 LCIS
    Python学习-2.安装IDE
    Python学习-1.安装Python
  • 原文地址:https://www.cnblogs.com/yhleng/p/11940020.html
Copyright © 2020-2023  润新知