【问题标题】:is their a way to run multiprocces of terminal command in linux from python是他们在 linux 中从 python 运行多进程终端命令的方法吗
【发布时间】:2022-01-19 11:48:57
【问题描述】:

我尝试运行此代码以将 .MOV 文件的文件夹的文件类型更改为 .mp4 文件我尝试在多处理中执行此操作,因为更改文件类型确实非常消耗 CPU。:

代码

def change_video_file_type_for_multy_proccecing(file_path):
   os.system('ffmpeg -i ' + file_path+ ' -vcodec h264 -acodec mp2 -q:v 0 ' + file_path.replace(".MOV",".mp4"))

input_for_after_trim = "/home/oem/Documents/2021/MOV/after_trim"
output_for_after_trim = "/home/oem/Documents/2021/mp4/after_trim"
file_path_list = []
for filename in os.listdir(input_for_after_trim):
   file_path_list.append(input_for_after_trim+"/"+filename)
with concurrent.futures.ProcessPoolExecutor() as executor:
   # other way
   results = executor.map(change_video_file_type_for_multy_proccecing,file_path_list)  # will return the result of the function by calling order

问题:

但是当我这样运行它但不知何故创建了一些文件但在早期停止并且它保存了一个 23 字节的文件意味着命令在开始时停止并且其他一些文件很好地改变了它们的文件类型。

这不是因为终端命令中的问题,因为当我同步时(没有多线程它运行良好但真的很慢)

我的电脑详情:

Linux Ubuntu 21.04 内核 5.11.0-41-generic

【问题讨论】:

    标签: python linux multithreading operating-system


    【解决方案1】:

    这能解决您的问题吗?

    from multiprocessing import Pool
    import os
    
    def change_video_file_type_for_multy_proccecing(file_path):
       os.system('ffmpeg -i ' + file_path+ ' -vcodec h264 -acodec mp2 -q:v 0 ' + file_path.replace(".MOV",".mp4"))
    
    input_for_after_trim = "/home/oem/Documents/2021/MOV/after_trim"
    output_for_after_trim = "/home/oem/Documents/2021/mp4/after_trim"
    file_path_list = []
    for filename in os.listdir(input_for_after_trim):
       file_path_list.append(input_for_after_trim+"/"+filename)
    
    num_processes = os.cpu_count() #just an example, set to what you want
    with Pool(num_processes) as p:
       results = list(p.map(change_video_file_type_for_multy_proccecing,file_path_list))
    
    

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2021-11-18
      • 2014-08-06
      • 2021-08-16
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多