python - 使用生成器对多个文件进行多重处理以及解决 TypeError ("can' t pickle 生成器对象的方法”)

标签 python multiprocessing generator

我正在尝试一次处理多个文件,其中每个文件将生成数据 block 以同时提供给具有特定大小限制的队列。 例如,如果有 5 个文件,每个文件包含 100 万个元素,我想将每个文件中的 100 个元素提供给另一个生成器,该生成器一次生成 500 个元素。

这是我到目前为止一直在尝试的方法,但遇到了无法pickle生成器错误:

import os
from itertools import islice
import multiprocessing as mp
import numpy as np

class File(object):
    def __init__(self, data_params):
        data_len = 100000
        self.large_data = np.array([data_params + str(i) for i in np.arange(0, data_len)])
    def __iter__(self):
        for i in self.large_data:
            yield i

def parse_file(file_path):
    # differnt filepaths yeild different data obviously
    # here we just emulate with something silly
    if file_path == 'elephant_file':
        p = File(data_params = 'elephant')
    if file_path == 'number_file':
        p = File(data_params = 'number')
    if file_path == 'horse_file':
        p = File(data_params = 'horse')


    yield from p

def parse_dir(user_given_dir, chunksize = 10):
    pool = mp.Pool(4)
    paths = ['elephant_file', 'number_file', 'horse_file'] #[os.path.join(user_given_dir, p) for p in os.listdir(user_given_dir)]

    # Works, but not simultaneously on all paths
#     for path in paths:
#         data_gen = parse_file(path)
#         parsed_data_batch = True
#         while parsed_data_batch:
#             parsed_data_batch = list(islice(data_gen, chunksize))
#             yield parsed_data_batch

    # Doesn't work
    for objs in pool.imap(parse_file, paths, chunksize = chunksize):
        for o in objs:
            yield o

it = parse_dir('.')
for ix, o in enumerate(it):
    print(o) # hopefully just prints 10 elephants, horses and numbers
    if ix>2: break

有人知道如何获得所需的行为吗?

最佳答案

  1. 对于pickle错误:

    parse_file 是一个生成器,而不是常规函数,因为它内部使用 yield

    并且多处理需要一个函数作为任务来执行。因此,您应该在 parse_file()

  2. 中将 yield from p 替换为 return p
  3. 如果您想从所有文件中逐个获取记录,请尝试在 parse_dir() 中使用 zip:

    iterators = [
        iter(e) for e in pool.imap(parse_file, paths, chunksize=chunksize)
    ]
    
    while True:
        batch = [
            o for i in iterators
            for _, o in zip(range(100), i)  # e.g., 100
        ]
       if batch:
            yield batch
        else:
            return
    

关于python - 使用生成器对多个文件进行多重处理以及解决 TypeError ("can' t pickle 生成器对象的方法”),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095679/

相关文章:

python - 为什么脚本语言不将 Unicode 输出到 Windows 控制台?

python - 模块 'tensorflow' 没有属性 'constant'

python - 为什么 multiprocessing.Queue 没有 task_done 方法

Python - 在多处理调用之前修改的全局变量作为原始状态传递

python - 在 Python 中查找打印语句

python - pyinstaller 和 matplotlib 超出最大递归深度

python - 如何将 "listen"到 Python 中的多处理队列

python - 如何避免在排序的嵌套循环生成器中进行不必要的计算?

python - python 生成器函数中引发的 StopIteration 异常处理

java - 用于 Java 的 Finagle Thrift 生成器