Python 多进程共享一个导入Class

Python Multi-process share an imported Class

我在 Python 导入多处理共享 Class 时遇到问题。麻烦的部分是这样的:

文件A:

class Meta:
    db_a = None
    db_b = None
    ...
# class will be initialized at the very beginning of the program and might
# be imported by all other models globally for a global variable/instance
# access, for example, a global DB access instance is in Meta

文件 B:

from file_A import Meta
def runner():
    initialize_meta_db()  # Meta's attributes now have values
    ...
    pool = multiprocessing.Pool(4)
    pool.map(worker, arg_list)
    pool.close()
    pool.join()
    ...

def worker(*args):
    ...
    print(Meta.db_a)  # process will print None
    ...
# a runner function which spawns 4 processes, each process will use class Meta
# to do some work.

但是程序运行出错,对于每个进程,Meta class 没有初始化,每个属性都是 None。我知道原因,Meta class 仅在主进程的内存中初始化,每个子进程将独立拥有自己的原始 class 元数据。

但是有什么方法可以让我与父进程和子进程共享这个 class 吗?谢谢!

您是否考虑过使用 multiprocessing.Pool 的初始化程序和 initargs 参数?

我稍微修改了你的代码来做到这一点并且能够 运行。它似乎做你想要的。

File_A

class Meta:
    db_a = None
    db_b = None
    @classmethod
    def initialize_meta_db(cls, db_a='a', db_b='b'):
        Meta.db_a = db_a
        Meta.db_b = db_b

File_B

import multiprocessing

from file_A import Meta

def runner():
    Meta.initialize_meta_db()  # Meta's attributes now have values
    pool = multiprocessing.Pool(4, init, '')
    pool.map(worker, (1, 2, 3, 4))
    pool.close()
    pool.join()

def init(*initargs):
    from file_A import Meta
    Meta.initialize_meta_db()

def worker(*args):
    print('Worker {} -- Work {}'.format(args, Meta.db_a))

if __name__ == '__main__':
    runner()