与此类似的东西应该可以解决问题:
import multiprocessing
...
def wrapper(currentfile):
#create your commandline -- for the sake of simplicity, I assume that
#the only thing that changes is the filename you are passing to
#HandBrakeCLI (and that the filename is the last argument to pass)
cmd='HandBrakeCLI -i ... -o ... '+currentfile
proc = subprocess.Popen(cmd)
proc.wait()
return 'foo'
files=os.listdir(path) #Any way that you build up your list of files is fine
output = multiprocessing.Pool(4).map(wrapper,files) #output is ['foo', 'foo', ..., 'foo']
当然,这使用了一个类似map
的函数,因为它的副作用是许多 python 人不喜欢的......但我发现它足够直观——尤其是如果你发表评论。我还让函数 return 'foo' 来证明你可以很容易地访问函数的返回值。