cancel - Stop function running in the background - MATLAB (original) (raw)
Main Content
Stop function running in the background
Syntax
Description
cancel([F](#mw%5F0f7e1c29-8f37-4104-9792-b526a0eb8d32))
stops each queued or running element of the Future
array F
.
For each element of F
that does not have theState
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 useparfeval
or parfevalOnAll
to run these functions, cancel
is unable to stop these futures. Use thedelete (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
.
Examples
Stop Functions Running in Background
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.
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.
The function is now in the 'finished'
state.
Input Arguments
F
— Input Future
parallel.Future
scalar | parallel.Future
array
Input Future
, specified as a parallel.Future
scalar or array.
Example: F = parfeval(backgroundPool,@magic,1,3);
Version History
Introduced in R2013b