Python多进程共享导入的类
我遇到了一个关于Python多处理共享导入类的问题。问题是这样的: 文件A:Python多进程共享导入的类,python,process,multiprocessing,python-multiprocessing,Python,Process,Multiprocessing,Python Multiprocessing,我遇到了一个关于Python多处理共享导入类的问题。问题是这样的: 文件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,
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
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.
文件B:
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
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
类未初始化,并且每个属性都是None
。我知道原因是,Meta
类只在主进程的内存中初始化,每个子进程将独立地拥有自己的原始类Meta
但是有什么方法可以让父进程和子进程共享这个类呢?谢谢 您是否考虑过使用multiprocessing.Pool的initializer和initargs参数 为了做到这一点,我稍微修改了您的代码,使其能够运行。它似乎做了你想做的事 档案室
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
档案室
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()
你应该阅读上面的文件。