如何一致地并行运行 Python 子进程?



我写了一个python脚本,可以使用mpg123mp3转换为wav。然后ffmpeg获取mpg123的输出并对其进行上采样。最后,ffmpeg输出文件上传到云。所有这些步骤必须一致地运行。

subprocess.run(['mpg123', ...])
subprocess.run(['ffmpeg', ...])
upload()

假设我有很多mp3文件,我想同时运行 10 个线程。我知道Python在subprocess.Popenthreadingconcurrent.futuresmultiprocessing模块中提供线程化。并行处理此过程的正确方法是什么?

您可以使用MPipe 库:

from mpipe import OrderedStage, Pipeline
def increment(value):
return value + 1
def double(value):
return value * 2
stage1 = OrderedStage(increment, 3)
stage2 = OrderedStage(double, 3)
pipe = Pipeline(stage1.link(stage2))
for number in range(10):
pipe.put(number)
pipe.put(None)
for result in pipe.results():
print(result)

最新更新