Here one with schedule which can more sense to use with long running task like this.
So it do a check if stream is running every 5-sec(check_process()) if it dos do nothing,else run_stream().
Kill the process here as test every 40-sec,then check_process will restart stream.
Lightweight as only charge OS at schedule time.
So it do a check if stream is running every 5-sec(check_process()) if it dos do nothing,else run_stream().
Kill the process here as test every 40-sec,then check_process will restart stream.
Lightweight as only charge OS at schedule time.
import subprocess import time, os import psutil import schedule import threading def check_process(): proc_name = "mpv.exe" for proc in psutil.process_iter(): if proc.name() == proc_name: return True else: return False def kill_stream(): proc_name = "mpv.exe" for pid in psutil.process_iter(): if pid.name() == proc_name: subprocess.run(f"TASKKILL /F /IM {pid.name()}") def run_stream(): MPV_EXE = "mpv.com" URL = 'https://www.radiantmediaplayer.com/media/bbb-360p.mp4' ARGS = [ MPV_EXE, "--no-video", URL ] with subprocess.Popen(ARGS) as proc: print(f'Running: {os.path.basename(URL)}') def check_stream(): if check_process(): pass else: print('Stream is no running --> restart') run_stream() def run_threaded(job_func): job_thread = threading.Thread(target=job_func) job_thread.start() if __name__ == '__main__': schedule.every(5).seconds.do(run_threaded, check_stream) schedule.every(40).seconds.do(run_threaded, kill_stream) # 3 hour restart #schedule.every(3).hours.do(run_threaded, kill_stream) while True: schedule.run_pending() time.sleep(1)