gpt4 book ai didi

python - 在多处理中创建 Singleton 类

转载 作者:行者123 更新时间:2023-11-30 22:33:44 25 4
gpt4 key购买 nike

我使用Metaclass创建Singleton类,它在多线程中工作良好并且只创建MySingleton类的一个实例,但在多处理中,它总是创建新实例

import multiprocessing

class SingletonType(type):
# meta class for making a class singleton
def __call__(cls, *args, **kwargs):

try:
return cls.__instance
except AttributeError:
cls.__instance = super(SingletonType, cls).__call__(*args, **kwargs)
return cls.__instance

class MySingleton(object):
# singleton class
__metaclass__ = SingletonType

def __init__(*args,**kwargs):
print "init called"


def task():
# create singleton class instance
a = MySingleton()


# create two process
pro_1 = multiprocessing.Process(target=task)
pro_2 = multiprocessing.Process(target=task)

# start process
pro_1.start()
pro_2.start()

我的输出:

init called
init called

我需要 MySingleton 类 init 方法仅被调用一次

最佳答案

每个子进程都运行自己的 Python 解释器实例,因此一个进程中的 SingletonType 不会与另一进程中的子进程共享其状态。这意味着仅存在于一个进程中的真正的单例将没有多大用处,因为您将无法在其他进程中使用它:虽然您可以 manually share data between processes ,但这仅限于基本数据类型(例如示例字典和列表)。

不依赖单例,只需在进程之间共享底层数据:

#!/usr/bin/env python3

import multiprocessing
import os


def log(s):
print('{}: {}'.format(os.getpid(), s))


class PseudoSingleton(object):

def __init__(*args,**kwargs):
if not shared_state:
log('Initializating shared state')
with shared_state_lock:
shared_state['x'] = 1
shared_state['y'] = 2
log('Shared state initialized')
else:
log('Shared state was already initalized: {}'.format(shared_state))


def task():
a = PseudoSingleton()


if __name__ == '__main__':
# We need the __main__ guard so that this part is only executed in
# the parent

log('Communication setup')
shared_state = multiprocessing.Manager().dict()
shared_state_lock = multiprocessing.Lock()

# create two process
log('Start child processes')
pro_1 = multiprocessing.Process(target=task)
pro_2 = multiprocessing.Process(target=task)
pro_1.start()
pro_2.start()

# Wait until processes have finished
# See https://stackoverflow.com/a/25456494/857390
log('Wait for children')
pro_1.join()
pro_2.join()

log('Done')

这会打印

16194: Communication setup
16194: Start child processes
16194: Wait for children
16200: Initializating shared state
16200: Shared state initialized
16201: Shared state was already initalized: {'x': 1, 'y': 2}
16194: Done

但是,根据您的问题设置,可能会使用其他进程间通信机制提供更好的解决方案。例如,Queue class 通常非常有用。

关于python - 在多处理中创建 Singleton 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45077043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com