execution#

Module providing additional execution methods.

Functions:

asynchronous(f)

Decorator for functions that are to execute asynchronously.

timeout_exec(obj, timeout[, args, kwargs])

Execute object with the timeout limit.

timeout_loop(obj, timeout[, args, kwargs, ...])

Loop while the specified object does not return the expected response.

ansys.cfx.core.utils.execution.asynchronous(f: Callable) Callable#

Decorator for functions that are to execute asynchronously. The decorated function returns a future object. Calling the result() method on the future object synchronizes the function execution.

Examples

Asynchronous execution using the @asynchronous decorator:

>>> import ansys.cfx.core as pycfx
>>> pysolve = pycfx.Solver.from_install(solver_input_file_name = <name1>)
>>> pysolve2 = pycfx.Solver.from_install(solver_input_file_name = <name2>)
>>> @asynchronous
... def asynchronous_solve(solver_session):
...     solver_session.solution.start_run()
>>> asynchronous_solve(pysolve)
<Future at 0x... state=running>

Using the asynchronous function directly:

>>> asynchronous(pysolve.solution.start_run)
<function SolverController.start_run at 0x...>

Synchronous execution of the previous two calls:

>>> asynchronous_solve(pysolve).result()
>>> asynchronous(pysolve2.solution.start_run)().result()
ansys.cfx.core.utils.execution.timeout_exec(obj, timeout, args=None, kwargs=None)#

Execute object with the timeout limit. This function tries to return whatever the provided object returns. If the object returns nothing, this function returns True. If it times out, it returns False.

Parameters:
objAny

Object to execute.

timeoutfloat

Time before cancelling execution and returning early.

argsAny, optional

Arguments to pass to the specified object.

kwargsAny, optional

Keyword arguments to pass to the specified object.

Examples

Execute time.sleep() for 2 seconds, with a timeout of 1 second:

>>> import time
>>> from ansys.cfx.core.utils.execution import timeout_exec
>>> ret = timeout_exec(time.sleep,timeout=1,args=(2,))
>>> assert ret is False
ansys.cfx.core.utils.execution.timeout_loop(obj: Any, timeout: float, args: Any | None = None, kwargs: Any | None = None, idle_period: float = 0.2, expected: str = 'truthy') Any#

Loop while the specified object does not return the expected response. A timeout occurs after the specified time elapses. This function tries to return whatever is returned by the specified object. If nothing is returned before the timeout, it returns the opposite of the expected value. For example, it returns True if expected == "falsy" and False if expected == "truthy".

Parameters:
objAny

Object to evaluate while looping if it does not return the expected response.

timeoutfloat

Time before cancelling execution and returning early.

argsAny, default: None

Arguments to pass to the specified object.

kwargsAny, default: None

Keyword arguments to pass to the specified object.

idle_periodfloat, default: 0.2

Time in seconds to wait between object evaluations.

expected: str, default: “truthy”

What type of return is expected. Options are "truthy" or "falsy". By default, "truthy" is the expected return from the specified object.

Raises:
InvalidArgument

If an unrecognized value is passed for expected.

Examples

Waiting 5 seconds to see if func("test") returns True:

>>> import time
>>> from ansys.cfx.core.utils.execution import timeout_loop
>>> def func(name):
...     return False
>>> func("test")
False
>>> response = timeout_loop(func, timeout=5.0, args=("test",))

Waiting 5 seconds to see if func2("test",word="hello") returns False:

>>> def func2(name1, word="name2"):
...     return True
>>> func2("test", word="hello")
True
>>> response = timeout_loop(func2, timeout=5.0, expected="falsy", args=("test",),
...                         kwargs={"word":"hello",})