【发布时间】:2019-02-05 15:39:10
【问题描述】:
我知道进程在 python 中不共享相同的上下文。但是单例对象呢?我能够让子进程与父进程共享相同的内部对象,但我无法理解如何。下面的代码有问题吗?
这可能是对this stackoverflow question 的跟进。
这是我的代码:
Singleton.py:
import os
class MetaSingleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Singleton:
__metaclass__ = MetaSingleton
def __init__(self):
self.key="KEY TEST"
print "inside init"
def getKey(self):
return self.key
def setKey(self,key1):
self.key = key1
process_singleton.py:
import os
from Singleton import Singleton
def callChildProc():
singleton = Singleton()
print ("singleton key: %s"%singleton.getKey())
def taskRun():
singleton = Singleton()
singleton.setKey("TEST2")
for i in range(1,10):
print ("In parent process, singleton key: %s" %singleton.getKey())
try:
pid = os.fork()
except OSError,e:
print("Could not create a child process: %s"%e)
if pid == 0:
print("In the child process that has the PID %d"%(os.getpid()))
callChildProc()
exit()
print("Back to the parent process")
taskRun()
【问题讨论】:
-
哪个操作系统?在类似 linux 的系统上,分叉的子进程具有父进程的写时复制视图。孩子看到分叉之前父母设置的任何内容。
-
哦,我明白了。这是否意味着,一旦启动子进程,如果父进程继续并更改某些值(例如,上面示例中的“键”),子进程会看到它吗?这是在linux上。本质上,单例行为是否也保留在子进程中?
-
我写了一个答案,希望 (?!) 解释它是如何工作的。
标签: python process singleton fork