Python:并行处理列表中的N个文件

标签 python list

<分区>

有一个文件列表,我正在用一些函数一个一个地处理:

list_of_files = [file_1, file_2, file_3, .... file_n]

# each file we are processing in function function_x(file)

for file in list_of_files:
   function_x(file)

但是处理一个文件花费的时间太长,所以我想并行处理 4 个文件,当其中任何一个完成后,继续下一个表单 list_of_files

最佳答案

尝试使用parallel map :

import multiprocessing
pool = multiprocessing.Pool()
pool.map(function_x, list_of_files)

关于Python:并行处理列表中的N个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532488/

相关文章:

Python (pySerial) : cannot trim/remove CR, LF 或字符串中的意外字符

r - 导出到 R 中列表的 JSON 列表

xcode - Swift 1.2 - 列表类型未声明

list - 在 Haskell 中取出列表中某个元素的最后一次出现

python:从互联网复制+粘贴撇号给了我一个奇怪的错误

python - 确保 numpy 数组中至少有一定的维度

python - Pandas 合并相似行的列值

python - pyInstaller 没有为 python3 安装

java.lang.IndexOutOfBoundsException :Invalid index x, 大小为 x

python 2 : AttributeError: 'list' object has no attribute 'strip'