Launching the audio processing in a separated thread

This program launches an audio process in a separated thread with the subprocess module and sends parameters to its standard input to control the sound playback.

import os
import sys
import time
import random
import subprocess

# Get the python command according to the current operating system.
if sys.platform.startswith("win"):
    PYTHON_EXE = "py -%d.%d" % (sys.version_info[0], sys.version_info[1])
    PYTHON_EXE = "python%d.%d" % (sys.version_info[0], sys.version_info[1])

# Path of the python file to run in the subprocess.
script_path = os.path.join(os.path.expanduser("~"), "")

# Thread's processing... Soundfile player + delay + reverb.
script = """
from pyo import *

s = Server(duplex=0).boot()

sf = SfPlayer('../snds/snd_1.aif', mul=0.7)
dd = SmoothDelay(sf, delay=0.25, feedback=0.5)
rv = STRev(sf+dd, inpos=0.50, revtime=1.5, cutoff=5000, bal=0.25).out()


# Create the python file to run in the subprocess.
f = open(script_path, "w")

# Launch an interactive python (-i flag) in a subprocess and store a
# reference to the standard input to pass message to the running process.
pipe = subprocess.Popen(
    ["%s -i %s" % (PYTHON_EXE, script_path)], shell=True, universal_newlines=True, stdin=subprocess.PIPE,

# Wait for the audio server to be ready.

# Send events to the subprocess.
for i in range(20):
    # Randomly choose a sound (snd_1.aif to snd_6.aif).
    snd = "../snds/snd_%d.aif" % random.randrange(1, 7)

    # Replace the sound and start the playback.
    pipe.write("sf.path = '%s'\ndump =\n" % snd)
    pipe.write("dd.delay = %f\n" % random.uniform(0.1, 0.5))

    # Wait some time before the next event.
    time.sleep(random.uniform(0.5, 1))

# Stop the audio Server before exiting.

# Close the subprocess.

# Delete the python file.