Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import time
- import signal
- if len(sys.argv) < 3:
- print("usage: throttle pid|name max_CPU_percent\ne.g throttle handbrake 25, limits handbrake to 25% CPU")
- sys.exit()
- pid = sys.argv[1]
- cpuPercent = int(sys.argv[2])
- if not pid.isdigit():
- processIDs = os.popen('pgrep -i %s' %(pid)).read().rstrip("\n")
- processIDList = filter(None, processIDs.split("\n"))
- if len(processIDList) == 1:
- pid = processIDList[0]
- elif len(processIDList) > 1:
- print("Matched more than one process: %s" %(processIDList))
- sys.exit(0)
- else:
- print("No matching process found for: %s" %(pid))
- sys.exit(0)
- pid = int(pid)
- sleepTime = 0.1 * (1 - (cpuPercent / 100.0))
- runTime = 0.1 * cpuPercent / 100.0
- def interrupt_handler(sentSignal, frame):
- os.kill(pid, signal.SIGCONT)
- exit(0)
- signal.signal(signal.SIGINT, interrupt_handler)
- while True:
- os.kill(pid, signal.SIGSTOP)
- time.sleep(sleepTime)
- os.kill(pid, signal.SIGCONT)
- time.sleep(runTime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement