Contenido principal

prodserver.addon.asyncfeval

R2026b

Create a function scheduled to run on MATLAB Production Server

Since R2026b

    Description

    F = prodserver.addon.asyncfeval(host,port,archive,fcn,tls,numFcnOut,X1,...,Xm) schedules the deployed function fcn to run in the background on MATLAB® Production Server™. You can run other code while MATLAB Production Server is running the function. MATLAB Production Server asynchronously evaluates the function with X1,X2,…,Xm input arguments and returns the prodserver.addon.FevalFuture object F. The request computes a number of outputs specified by numFcnOut and stores them on the server.

    MATLAB Production Server immediately returns the prodserver.addon.FevalFuture object F. You can use fetchOutputs to retrieve the results from the FevalFuture. To stop running the function, use the cancel function.

    example

    F = prodserver.addon.asyncfeval(uri,numFcnOut,X1,...,Xm) specifies a server host, port, archive, and function using a network address string.

    example

    Examples

    collapse all

    Run a function in the background on MATLAB Production Server. When you run a function in the background, You can run other client code while MATLAB Production Server is processing the asynchronous request.

    Create a function called addmatrix that takes two matrices as inputs.

    function a = addmatrix(a1,a2)
    a = a1 + a2;

    Create a MATLAB Production Server archive named services containing the addmatrix function.

    Use asyncfeval to run the function addmatrix on a MATLAB Production Server instance running on localhost:9910 and retrieve one output.

    a1 = [1 2 3]
    a2 = [4 5 6]
    addFuture = prodserver.addon.asyncfeval("localhost", 9910,"services","addmatrix", false, 1, a1, a2);

    To retrieve the output from the background, use fetchOutputs. MATLAB returns the output after the execution of addmatrix is complete.

    fetchOutputs(addFuture)
    fetchOutputs(addFuture) =
    
         5     7     9

    For more information on creating a server instance, see Create Server Instance Using Command Line.

    To stop a MATLAB function that you are running in the background on MATLAB Production Server, use the cancel function.

    Create a wrapper for the pause (MATLAB) function called mypause.

    Package the mypause function in a deployable archive called services. Upload the deployable archive to an instance of MATLAB Production Server running on localhost:9910.

    Use asyncfeval to run mypause(100) on MATLAB Production Server.

    pauseFuture = prodserver.addon.asyncfeval("http://localhost:9910/services/mypause", 0, 100);

    When you use asyncfeval, you create an FevalFuture object. Check the state of the FevalFuture object by accessing its State property.

    pauseFuture.State
    ans = 
    'Processing'
    

    To stop the function from running in the background, cancel the FevalFuture object.

    cancel(pauseFuture)
    pauseFuture.State
    ans = 
    'Cancelled'
    

    The function is now in the 'Cancelled' state.

    Input Arguments

    collapse all

    Host name of the server that hosts the deployable archive containing the function to invoke, specified as a character vector or string scalar.

    Example: "144.213.5.7"

    Port number of the server that hosts a deployable archive containing the function to invoke, specified as a positive integer.

    Example: 9920

    Network address of the deployed function to invoke, specified as a character vector, string scalar, or matlab.net.uri.

    Example: "https://localhost:9910/services/fcn1"

    Name of the deployable archive containing the function to invoke, specified as a character vector or string scalar.

    Example: "services"

    Name of the function to invoke, specified as a character vector or string scalar.

    Example: "fcn1"

    Whether to use transport layer security (HTTPS), specified as a numeric or logical 1 (true) or 0 (false)..

    Example: true

    Data Types: logical

    Number of outputs to request from the invoked function, specified as a positive integer.

    Example: 3

    Data Types: double

    Input arguments for the function fcn, specified as a comma-separated list of variables or expressions.

    Example: 17, "test", datetime('now')

    Output Arguments

    collapse all

    Output future object, returned as a prodserver.addon.FevalFuture object.

    Use fetchOutputs to retrieve results from F.

    Version History

    Introduced in R2026b