+# ==============================================================================
+def MultiFonction(
+ __xserie,
+ _extraArguments = None,
+ _sFunction = lambda x: x,
+ _mpEnabled = False,
+ _mpWorkers = None,
+ ):
+ """
+ Pour une liste ordonnée de vecteurs en entrée, renvoie en sortie la liste
+ correspondante de valeurs de la fonction en argument
+ """
+ # Vérifications et définitions initiales
+ # logging.debug("MULTF Internal multifonction calculations begin with function %s"%(_sFunction.__name__,))
+ if not PlatformInfo.isIterable( __xserie ):
+ raise TypeError("MultiFonction not iterable unkown input type: %s"%(type(__xserie),))
+ if _mpEnabled:
+ if (_mpWorkers is None) or (_mpWorkers is not None and _mpWorkers < 1):
+ __mpWorkers = None
+ else:
+ __mpWorkers = int(_mpWorkers)
+ try:
+ import multiprocessing
+ __mpEnabled = True
+ except ImportError:
+ __mpEnabled = False
+ else:
+ __mpEnabled = False
+ __mpWorkers = None
+ #
+ # Calculs effectifs
+ if __mpEnabled:
+ _jobs = []
+ if _extraArguments is None:
+ _jobs = __xserie
+ elif _extraArguments is not None and isinstance(_extraArguments, (list, tuple, map)):
+ for __xvalue in __xserie:
+ _jobs.append( [__xvalue, ] + list(_extraArguments) )
+ else:
+ raise TypeError("MultiFonction extra arguments unkown input type: %s"%(type(_extraArguments),))
+ # logging.debug("MULTF Internal multiprocessing calculations begin : evaluation of %i point(s)"%(len(_jobs),))
+ import multiprocessing
+ with multiprocessing.Pool(__mpWorkers) as pool:
+ __multiHX = pool.map( _sFunction, _jobs )
+ pool.close()
+ pool.join()
+ # logging.debug("MULTF Internal multiprocessing calculation end")
+ else:
+ # logging.debug("MULTF Internal monoprocessing calculation begin")
+ __multiHX = []
+ if _extraArguments is None:
+ for __xvalue in __xserie:
+ __multiHX.append( _sFunction( __xvalue ) )
+ elif _extraArguments is not None and isinstance(_extraArguments, (list, tuple, map)):
+ for __xvalue in __xserie:
+ __multiHX.append( _sFunction( __xvalue, *_extraArguments ) )
+ elif _extraArguments is not None and isinstance(_extraArguments, dict):
+ for __xvalue in __xserie:
+ __multiHX.append( _sFunction( __xvalue, **_extraArguments ) )
+ else:
+ raise TypeError("MultiFonction extra arguments unkown input type: %s"%(type(_extraArguments),))
+ # logging.debug("MULTF Internal monoprocessing calculation end")
+ #
+ # logging.debug("MULTF Internal multifonction calculations end")
+ return __multiHX
+