Advertisement
Guest User

Throttle Mac app CPU usage

a guest
Dec 22nd, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import signal
  5.  
  6. if len(sys.argv) < 3:
  7.     print("usage: throttle pid|name max_CPU_percent\ne.g throttle handbrake 25, limits handbrake to 25% CPU")
  8.     sys.exit()
  9.  
  10. pid = sys.argv[1]
  11. cpuPercent = int(sys.argv[2])
  12.  
  13. if not pid.isdigit():
  14.     processIDs = os.popen('pgrep -i %s' %(pid)).read().rstrip("\n")
  15.     processIDList = filter(None, processIDs.split("\n"))
  16.     if len(processIDList) == 1:
  17.         pid = processIDList[0]
  18.     elif len(processIDList) > 1:
  19.         print("Matched more than one process: %s" %(processIDList))
  20.         sys.exit(0)
  21.     else:
  22.         print("No matching process found for: %s" %(pid))
  23.         sys.exit(0)
  24.    
  25. pid = int(pid)
  26.  
  27. sleepTime = 0.1 * (1 - (cpuPercent / 100.0))
  28. runTime = 0.1 * cpuPercent / 100.0
  29.  
  30. def interrupt_handler(sentSignal, frame):
  31.     os.kill(pid, signal.SIGCONT)
  32.     exit(0)
  33.  
  34. signal.signal(signal.SIGINT, interrupt_handler)
  35.  
  36. while True:
  37.     os.kill(pid, signal.SIGSTOP)
  38.     time.sleep(sleepTime)
  39.     os.kill(pid, signal.SIGCONT)
  40.     time.sleep(runTime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement