Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Touble with subprocess
#1
Hello, I am trying to write a script that calls a program to play audio and then restart the program so often.

However the program is not being killed and respawned?!

"""
Playback an audio stream.

Here we want to playback audio untill the user
presses ctrl+c.  If we do not restart our playback
process every three hours the server providing
the audio may start giving us silence.
"""
import random
import subprocess
import time


def make_url():
    """Append a random value to urls end."""
    rnd = str(random.randrange(1000000))
    url_with_rnd = "https://mediaserver3.afa.net:8443/talk.mp3" + "?" + rnd
    return url_with_rnd


MPV_EXE = "F:/z0bin/mpv/mpv-0-32-0/mpv.com"

while True:
    URL = make_url()

    ARGS = [
        MPV_EXE,
        "--no-config",
        "--input-conf=./input.conf",
        "--audio-device=wasapi/{9c3e3659-8106-448c-a9b3-93881bbbfadc}",
        "--audio-exclusive=no",
        "--input-media-keys=no",
        "--no-input-default-bindings",
        "--no-video",
        URL,
    ]

    with subprocess.run(ARGS) as proc:

        # Use 30 seconds to test if exiting works.
        # Else we'd use 10800 seconds.
        TIME_TO_EXIT = time.time() + 30

        while proc.poll() is not None:
            if time.time() > TIME_TO_EXIT:
                proc.terminate()
                print("")
                print("We exited MPV.")
                print("")
                break

            time.sleep(1)

        else:
            print("")
            print("MPV exited itself.")
            print("")
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