在多个文件上使用生成器进行多处理,并围绕TypeError(“无法使生成器对象腌制”)进行处理 - python

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

到目前为止,这是我一直在尝试的操作,但是遇到了can't pickle generator错误:

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

任何人都对如何获得所需的行为有任何想法?

参考方案

对于泡菜错误:

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

并且multiprocessing需要一个函数作为任务来执行。因此,您应该在yield from p中用return p替换parse_file()
如果要从所有文件中逐块读取记录,请尝试在zip中使用parse_dir()

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

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…