我在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
类,每个属性都是None
。我知道为什么Meta
类只在主进程的内存中初始化,并且每个子进程将独立拥有自己的原始类Meta。
但有什么方法可以与父进程和子进程共享此类?谢谢!
答案 0 :(得分:1)
您是否考虑过使用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()