map#
- LocalMechanicalPool.map(func, iterable=None, clear_at_start=True, progress_bar=True, close_when_finished=False, timeout=None, wait=True)#
Run a user-defined function on each Mechanical instance in the pool.
- Parameters:
- func
function
Function with
mechanical
as the first argument. The subsequent arguments should match the number of items in each iterable (if any).- iterable
list
,tuple
,optional
An iterable containing a set of arguments for the function. The default is
None
, in which case the function runs once on each instance of Mechanical.- clear_at_startbool,
optional
Clear Mechanical at the start of execution. The default is
True
. Setting this toFalse
might lead to instability.- progress_barbool,
optional
Whether to show a progress bar when running the batch of input files. The default is
True
, but the progress bar is not shown whenwait=False
.- close_when_finishedbool,
optional
Whether to close the instances when the function finishes running on all instances in the pool. The default is
False
.- timeout
float
,optional
Maximum runtime in seconds for each iteration. The default is
None
, in which case there is no timeout. If you specify a value, each iteration is allowed to run only this number of seconds. Once this value is exceeded, the batch process is stopped and treated as a failure.- waitbool,
optional
Whether block execution must wait until the batch process is complete. The default is
True
.
- func
- Returns:
list
A list containing the return values for the function. Failed runs do not return an output. Because return values are not necessarily in the same order as the iterable, you might want to add some sort of tracker to the return of your function.
Examples
Run several input files while storing the final routine. Note how the function to map must use
mechanical
as the first argument. The function can have any number of additional arguments.>>> from ansys.mechanical.core import LocalMechanicalPool >>> pool = LocalMechanicalPool(10) >>> completed_indices = [] >>> def function(mechanical, name, script): # name, script = args mechanical.clear() output = mechanical.run_python_script(script) return name, output >>> inputs = [("first","2+3"), ("second", "3+4")] >>> output = pool.map(function, inputs, progress_bar=False, wait=True) [('first', '5'), ('second', '7')]