Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Touble with subprocess
#7
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.
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)
Reply


Messages In This Thread
Touble with subprocess - by jake9wi - Apr-12-2020, 11:47 PM
RE: Touble with subprocess - by Larz60+ - Apr-13-2020, 12:45 AM
RE: Touble with subprocess - by snippsat - Apr-13-2020, 11:15 AM
RE: Touble with subprocess - by jake9wi - Apr-13-2020, 07:15 PM
RE: Touble with subprocess - by snippsat - Apr-13-2020, 11:06 PM
RE: Touble with subprocess - by steve_shambles - Apr-14-2020, 03:36 AM
RE: Touble with subprocess - by snippsat - Apr-14-2020, 08:51 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020