execution#
Module providing additional execution methods.
Functions:
|
Decorator for functions that are to execute asynchronously. |
|
Execute object with the timeout limit. |
|
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 returnsFalse.- Parameters:
- obj
Any Object to execute.
- timeout
float Time before cancelling execution and returning early.
- args
Any,optional Arguments to pass to the specified object.
- kwargs
Any,optional Keyword arguments to pass to the specified object.
- obj
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
Trueifexpected == "falsy"andFalseifexpected == "truthy".- Parameters:
- obj
Any Object to evaluate while looping if it does not return the expected response.
- timeout
float Time before cancelling execution and returning early.
- args
Any, default:None Arguments to pass to the specified object.
- kwargs
Any, default:None Keyword arguments to pass to the specified object.
- idle_period
float, 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.
- obj
- Raises:
InvalidArgumentIf an unrecognized value is passed for
expected.
Examples
Waiting 5 seconds to see if
func("test")returnsTrue:>>> 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")returnsFalse:>>> 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",})