如何初始化具有共享状态的 python 多进程工作线程池?

How to initialize a pool of python multiprocessing workers with a shared state?

我正在尝试并行执行 some machine learning algorithm

当我使用多处理时,它比不使用时慢。我的疯狂猜测是我使用的模型的 pickle 序列化减慢了整个过程。所以问题是:如何用初始状态初始化池的工作程序,这样我就不需要在每次调用模型时都serialize/deserialize?

这是我当前的代码:

import pickle
from pathlib import Path
from collections import Counter
from multiprocessing import Pool

from gensim.models.doc2vec import Doc2Vec

from wikimark import html2paragraph
from wikimark import tokenize


def process(args):
    doc2vec, regressions, filepath = args
    with filepath.open('r') as f:
        string = f.read()
    subcategories = Counter()
    for index, paragraph in enumerate(html2paragraph(string)):
        tokens = tokenize(paragraph)
        vector = doc2vec.infer_vector(tokens)
        for subcategory, model in regressions.items():
            prediction = model.predict([vector])[0]
            subcategories[subcategory] += prediction
    # compute the mean score for each subcategory
    for subcategory, prediction in subcategories.items():
        subcategories[subcategory] = prediction / (index + 1)
    # keep only the main category
    subcategory = subcategories.most_common(1)[0]
    return (filepath, subcategory)


def main():
    input = Path('./build')
    doc2vec = Doc2Vec.load(str(input / 'model.doc2vec.gz'))
    regressions = dict()
    for filepath in input.glob('./*/*/*.model'):
        with filepath.open('rb') as f:
            model = pickle.load(f)
        regressions[filepath.parent] = model

    examples = list(input.glob('../data/wikipedia/english/*'))

    with Pool() as pool:
        iterable = zip(
            [doc2vec] * len(examples),  # XXX!
            [regressions] * len(examples),  # XXX!
            examples
        )
        for filepath, subcategory in pool.imap_unordered(process, iterable):
            print('* {} -> {}'.format(filepath, subcategory))


if __name__ == '__main__':
    main()

标有XXX!的行指向我调用pool.imap_unodered时序列化的数据。至少有 200MB 的数据被序列化。

如何避免序列化?

解决方案就像对 doc2vecregressions 使用全局一样简单。