#!/usr/bin/env python3
"""Duty-cycle the Kodi process with SIGSTOP/SIGCONT: stand-in for a TV `factor` times slower. throttle.py <factor>"""
import os, signal, subprocess, sys, time
factor=float(sys.argv[1]); slot=0.02
run, stop = slot, slot*(factor-1)
while True:
    p=subprocess.run(['pgrep','-x','Kodi'],capture_output=True,text=True).stdout.split()
    if not p: time.sleep(.05); continue
    pid=int(p[0])
    try:
        while True:
            os.kill(pid,signal.SIGSTOP); time.sleep(stop)
            os.kill(pid,signal.SIGCONT); time.sleep(run)
    except ProcessLookupError:
        pass
