From a650e8469fef667c2c4d20a0b3bb55e301a9e909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 17 Mar 2016 22:56:58 +0100 Subject: [PATCH] Speed up pool setup We split the operations into two steps, so we can do the setup calls at the end if there is no installation or update. issue5302 review17041002 --- doc/ref/pool.rst | 11 ++++++++--- trytond/modules/__init__.py | 11 +++++++++-- trytond/pool.py | 21 +++++++++++++++------ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/doc/ref/pool.rst b/doc/ref/pool.rst index 0ca56384dc..88f42bcea3 100644 --- a/doc/ref/pool.rst +++ b/doc/ref/pool.rst @@ -39,10 +39,15 @@ Instance methods: Return an interator over instances names. -.. method:: Pool.setup(module) +.. method:: Pool.fill(module) - Setup classes for module and return a list of classes for each type in a - dictionary. + Fill the pool with the registered class from the module and return a list + of classes for each type in a dictionary. + +.. method:: Pool.setup([classes]) + + Call all setup methods of the classes provided or for all the registered + classes. ======== PoolMeta diff --git a/trytond/modules/__init__.py b/trytond/modules/__init__.py index 6a31aecfe8..9a453efaa0 100644 --- a/trytond/modules/__init__.py +++ b/trytond/modules/__init__.py @@ -217,10 +217,13 @@ def load_module_graph(graph, pool, update=None, lang=None): if module not in MODULES: continue logger.info(module) - classes = pool.setup(module) + classes = pool.fill(module) + if update: + pool.setup(classes) package_state = module2state.get(module, 'uninstalled') if (is_module_to_install(module, update) - or package_state in ('to install', 'to upgrade')): + or (update + and package_state in ('to install', 'to upgrade'))): if package_state not in ('to install', 'to upgrade'): if package_state == 'installed': package_state = 'to upgrade' @@ -281,6 +284,9 @@ def load_module_graph(graph, pool, update=None, lang=None): Transaction().connection.commit() + if not update: + pool.setup() + for model_name in models_to_update_history: model = pool.get(model_name) if model._history: @@ -291,6 +297,7 @@ def load_module_graph(graph, pool, update=None, lang=None): while modules_todo: (module, to_delete) = modules_todo.pop() convert.post_import(pool, module, to_delete) + logger.info('all modules loaded') def get_module_list(): diff --git a/trytond/pool.py b/trytond/pool.py index 6101967eb6..3b9a6eebf7 100644 --- a/trytond/pool.py +++ b/trytond/pool.py @@ -196,10 +196,10 @@ class Pool(object): ''' return self._pool[self.database_name][type].iteritems() - def setup(self, module): + def fill(self, module): ''' - Setup classes for module and return a list of classes for each type in - a dictionary. + Fill the pool with the registered class from the module. + Return a list of classes for each type in a dictionary. ''' classes = {} for type_ in self.classes.keys(): @@ -212,13 +212,22 @@ class Pool(object): pass if not issubclass(cls, PoolBase): continue - cls.__setup__() self.add(cls, type=type_) classes[type_].append(cls) - for cls in classes[type_]: - cls.__post_setup__() return classes + def setup(self, classes=None): + logger.info('setup pool for "%s"', self.database_name) + if classes is None: + classes = {} + for type_ in self._pool[self.database_name]: + classes[type_] = self._pool[self.database_name][type_].values() + for type_, lst in classes.iteritems(): + for cls in lst: + cls.__setup__() + for cls in lst: + cls.__post_setup__() + def isregisteredby(obj, module, type_='model'): pool = Pool()