From 87d1254b2c1c2e9f4bdd18f595ae8fd4a756a33d Mon Sep 17 00:00:00 2001 From: Griatch Date: Wed, 14 May 2014 20:07:59 +0200 Subject: [PATCH] Changed contrib/procpool's PROC_MODIFIED_OBJS list to sit in src.utils.idmapper.base as well as be a WeakValueDictionary instead of a normal list. This removes unnecessary reference counts to objects. --- contrib/procpools/python_procpool.py | 7 ++++--- src/__init__.py | 3 --- src/utils/idmapper/base.py | 13 +++++++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/contrib/procpools/python_procpool.py b/contrib/procpools/python_procpool.py index b77a1c7407..1a1e483fb7 100644 --- a/contrib/procpools/python_procpool.py +++ b/contrib/procpools/python_procpool.py @@ -32,9 +32,9 @@ from twisted.protocols import amp from twisted.internet import threads from contrib.procpools.ampoule.child import AMPChild from src.utils.dbserialize import to_pickle, from_pickle, do_pickle, do_unpickle +from src.utils.idmapper.base import PROC_MODIFIED_OBJS from src.utils.utils import clean_object_caches, to_str from src.utils import logger -from src import PROC_MODIFIED_OBJS # @@ -140,7 +140,7 @@ class PythonProcPoolChild(AMPChild): exec source in available_vars ret = _return.get_returns() # get the list of affected objects to recache - objs = list(set(PROC_MODIFIED_OBJS)) + objs = PROC_MODIFIED_OBJS.values() # we need to include the locations too, to update their content caches objs = objs + list(set([o.location for o in objs if hasattr(o, "location") and o.location])) @@ -151,7 +151,8 @@ class PythonProcPoolChild(AMPChild): else: to_recache = "" # empty the list without loosing memory reference - PROC_MODIFIED_OBJS[:] = [] + #PROC_MODIFIED_OBJS[:] = [] + PROC_MODIFIED_OBJS.clear() #TODO - is this not messing anything up? return {'response': ret, 'recached': to_recache} ExecuteCode.responder(executecode) diff --git a/src/__init__.py b/src/__init__.py index 54e1114f71..e69de29bb2 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,3 +0,0 @@ -# experimental central dictionary for models in -# subprocesses to report they have been changed. -PROC_MODIFIED_OBJS = [] diff --git a/src/utils/idmapper/base.py b/src/utils/idmapper/base.py index cbf17b4063..9db5d1625c 100755 --- a/src/utils/idmapper/base.py +++ b/src/utils/idmapper/base.py @@ -25,10 +25,11 @@ _GA = object.__getattribute__ _SA = object.__setattr__ _DA = object.__delattr__ - # determine if our current pid is different from the server PID (i.e. -# if we are in a subprocess or not) -from src import PROC_MODIFIED_OBJS +# if we are in a subprocess or not); Changes are stored here so the +# main process can be informed to update itself. +PROC_MODIFIED_COUNT = 0 +PROC_MODIFIED_OBJS = WeakValueDictionary() # get info about the current process and thread _SELF_PID = os.getpid() @@ -36,6 +37,8 @@ _SERVER_PID, _PORTAL_PID = get_evennia_pids() _IS_SUBPROCESS = (_SERVER_PID and _PORTAL_PID) and not _SELF_PID in (_SERVER_PID, _PORTAL_PID) _IS_MAIN_THREAD = threading.currentThread().getName() == "MainThread" + + #_SERVER_PID = None #_PORTAL_PID = None # #global _SERVER_PID, _PORTAL_PID, _IS_SUBPROCESS, _SELF_PID @@ -259,7 +262,9 @@ class SharedMemoryModel(Model): if _IS_SUBPROCESS: # we keep a store of objects modified in subprocesses so # we know to update their caches in the central process - PROC_MODIFIED_OBJS.append(cls) + global PROC_MODIFIED_COUNT, PROC_MODIFIED_OBJS + PROC_MODIFIED_COUNT += 1 + PROC_MODIFIED_OBJS[PROC_MODIFIED_COUNT] = cls if _IS_MAIN_THREAD: # in main thread - normal operation