主要内容

parfeval

Run function in background

    Description

    example

    F= parfeval(backgroundPool,fcn,n,X1,...,Xm)schedules the functionfcn在背景中运行。您可以在matlab时运行其他代码®正在运行该功能fcn

    To run a function on a parallel pool, seeparfeval(Parallel Computing Toolbox)

    parfeval运行该功能fcnon a background worker. For more information about workers and the background, seeBackground Workers

    Matlab评估该功能fcnasynchronously as[Y1,...,Yn] = fcn(X1,...,Xm), withminputs andnoutputs.

    MATLAB returns the未来objectFbefore the functionfcnfinishes running. You can usefetchOutputs检索结果[Y1,...,Yn]from the future. To stop running the functionfcn, use thecancelfunction. For more information about futures, see未来

    F= parfeval(fcn,n,X1,...,Xm)schedules the functionfcn运行。

    MATLAB returns the未来objectFbefore the functionfcnfinishes running.

    Use this syntax to run code designed for use with Parallel Computing Toolbox™.

    • If you do not have Parallel Computing Toolbox, you do not use any parallel resources. The function runs in serial.

    • If you have Parallel Computing Toolbox,parfevalautomatically uses parallel resources.

    Examples

    collapse all

    This example shows how to run a function in the background usingparfevalBackgroundgool.。在后台运行函数时,您可以同时运行其他MATLAB代码。

    Useparfeval运行该功能魔法(3)和retrieve one output. SpecifyBackgroundgool.作为在后台运行函数的第一个参数。当你使用时parfeval,你创造一个未来目的。

    f = parfeval(backgroundPool,@magic,1,3);

    To retrieve the output from the background, usefetchOutputs。MATLAB returns the output once the execution of魔法is complete.

    fetchOutputs(f)
    ans =3×38 1 6 3 5 7 4 9 2

    This example shows how to stop a MATLAB function that you are running in the background. When you useparfeval跑步a function in the background, MATLAB immediately returns a未来目的。Long-running functions can block other functions from running in the background. To stop the function from running, you must use thecancelfunction instead of selectingLive Editor>Run > Stop

    Useparfeval跑步pause(Inf)without retrieving any outputs. SpecifyBackgroundgool.作为在后台运行函数的第一个参数。当你使用时parfeval,你创造一个未来目的。

    f = parfeval(backgroundPool,@pause,0,Inf);

    Check the state of the未来目的。

    F.State.
    ans = 'running'

    当你运行时parfeval, you schedule a function to run in the background. When the background pool has insufficient resources to run the function, the未来is in the'queued'状态。当函数由后台池运行时,未来is in the'running'状态。

    To stop the function from running in the background, cancel the未来目的。

    取消(f)f.state
    ans = 'finished'

    该功能现在在'完成的'状态。

    Input Arguments

    collapse all

    运行功能, specified as a function handle.

    Example:fcn = @magic

    Number of output arguments, specified as an nonnegative integer scalar.

    nis the number of output arguments expected from runningFCN(X1,...,XM)

    Data Types:single|double|int8|INT16|INT32.|int64|uint8|uint16|uint32|uint64

    输入参数,指定为逗号分隔的变量或表达式列表。

    Output Arguments

    collapse all

    Output未来, returned as aparallel.Future目的。

    Extended Capabilities

    See Also

    (Parallel Computing Toolbox)|||||||