[Tutor] Can subprocess run functions ?

Damon Timm damontimm at gmail.com
Thu Jan 8 01:02:48 CET 2009


Hi everyone - I was playing with subprocess (with some success, I
might add) to implement threading in my script (audio conversion).  My
goal is to be able to spawn off threads to make use of my
multiprocessor system (and speed up encoding).  With your help, I was
successful.

Anyhow, subprocess is working -- but I wonder if there is a way I can
send the entire *function* into its own subprocess ?  Because, in my
case, I want my function to: [a] convert files, [b] tag files, [c] do
some other stuff to files. Steps [b] and [c] require step [a] to be
complete ... but the minute I spawn off step [a] it acts like it is
already done (even though it is still working) ... I was hoping they
could all run in a single thread, one after another ...

I tried just giving subprocess.Popen the function name (rather than
the external program) but that didn't work; and I read through the
docs over at python.org ... but I can't find my answer.

With the code I have, I am not sure how to both wait for my subprocess
to finish (in the function) and allow the multithreading bit to work
together ... I have experimented myself but didn't really get
anywhere.  I commented in where I want to "do other stuff" before it
finishes ... wonder if you can take a look and show me where I may try
to head next?

Thanks!
------------------
import time
import subprocess

totProcs = 2 #number of processes to spawn before waiting
flacFiles = [["test.flac","test.mp3"],["test2.flac","test2.mp3"],\
        ["test3.flac","test3.mp3"],["test4.flac","test4.mp3"],\
        ["test5.flac","test5.mp3"],["test6.flac","test6.mp3"]]
procs = []

def flac_to_mp3(flacfile,mp3file):
    print "beginning to process " + flacfile
    p = subprocess.Popen(["flac","--decode","--stdout","--silent",flacfile],
stdout=subprocess.PIPE)
    p1 = subprocess.Popen(["lame","--silent","-",mp3file], stdin=p.stdout)

    # I want to do more stuff before this function ends, but need to
wait for p1 to finish first;
    # and, at the same time, I need to "return" p1 so the while loop
(below) works [I think]

    return p1

while flacFiles or procs:
    procs = [p for p in procs if p.poll() is None]
    while flacFiles and len(procs) < totProcs:
        file = flacFiles.pop(0)
        procs.append(flac_to_mp3(file[0],file[1]))
    time.sleep(1)


More information about the Tutor mailing list