This example shows how to call the MATLAB® sqrt
function
asynchronously from Python® and retrieve the square root later.
The engine calls MATLAB functions synchronously by default. Control returns to Python only when the MATLAB function finishes. But the engine also can call functions asynchronously. Control immediately returns to Python while MATLAB is still executing the function. The engine stores the result in a Python variable that can be inspected after the function finishes.
Use the background
argument to call a MATLAB function asynchronously.
import matlab.engine eng = matlab.engine.start_matlab() future = eng.sqrt(4.0,background=True) ret = future.result() print(ret)
2.0
Use the done
method to check if an
asynchronous call finished.
tf = future.done() print(tf)
True
To stop execution of the function before it finishes, call future.cancel()
.
matlab.engine.FutureResult
| matlab.engine.MatlabEngine