>我在 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, 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。
但是有什么方法可以与父进程和子进程共享此类吗?谢谢!
您是否考虑过使用多处理的初始值设定项和初始参数。池?
我稍微修改了您的代码以执行此操作并能够运行。它似乎做了你想做的事。
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()