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

"""
runing 100
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 = 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)

    print('\n====================占用CPU核数{}.===================='.format(cpu_logical_count))
    print('\n====================内存预计占用{}MB.================='.format(memory_used_mb))
    print('\n资源浪费starting......')
    print(cpu_sleep_time)

    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()

        # 内存占用
        s = ' ' * (memory_used_mb * 1024 * 1024)
    except KeyboardInterrupt:
        print("手工结束!")
12-31 19:50