Main Content

cancel

Stop function running in the background

    Syntax

    Description

    cancel(F) stops each queued or running element of the Future array F.

    For each element of F that does not have the State property 'finished', cancel sets the State property to 'finished'. All cancelled Future elements indicate cancelled execution in the Error property.

    You are unable to interrupt some functions and operations by pressing Ctrl+C, such as save. When you use parfeval or parfevalOnAll to run these functions, cancel is unable to stop these futures. Use the delete (Parallel Computing Toolbox) to shut down the pool and force MATLAB® to stop any running functions and operations.

    Note

    Using delete with the background pool is not recommended. Use instead cancelAll.

    example

    Examples

    collapse all

    This example shows how to stop a MATLAB function that you are running in the background. When you use parfeval to run a function in the background, MATLAB immediately returns a Future object. Long-running functions can block other functions from running in the background. To stop the function from running, you must use the cancel function instead of selecting Live Editor > Run > Stop.

    Use parfeval to run pause(Inf) without retrieving any outputs. Specify backgroundPool as the first argument to run the function in the background. When you use parfeval, you create a Future object.

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

    Check the state of the Future object.

    f.State
    ans = 
    'running'
    

    When you run parfeval, you schedule a function to run in the background. When the background pool has insufficient resources to run the function, the Future is in the 'queued' state. When the function is run by the background pool, the Future is in the 'running' state.

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

    cancel(f)
    f.State
    ans = 
    'finished'
    

    The function is now in the 'finished' state.

    Input Arguments

    collapse all

    Input Future, specified as a parallel.Future scalar or array.

    Example: F = parfeval(backgroundPool,@magic,1,3);

    Version History

    Introduced in R2013b

    See Also

    (Parallel Computing Toolbox) | | | | |