parfeval - Run function in background - MATLAB (original) (raw)

Run function in background

Syntax

Description

[F](#mw%5Fd398454b-bf36-4b2f-969f-bf8ed938afd1) = parfeval(backgroundPool,[fcn](#mw%5F67bd9e88-7c74-487b-902a-b1096bb2c249),[numFcnOut](#mw%5F6353cac3-5336-4646-a953-f4df69429b5c),[X1,...,Xm](#mw%5F7ea4f1da-203c-41d1-b794-a429c0f3a8fe)) schedules the function fcn to run in the background. You can run other code while MATLAB® is running the function fcn.

To run a function on a parallel pool, see parfeval (Parallel Computing Toolbox).

parfeval runs the function fcn on a background worker. For more information about workers and the background, see Background Workers.

MATLAB asynchronously evaluates the function fcn on each worker with X1,X2,…,Xm input arguments and returns numFcnOut output arguments.

MATLAB returns the Future object F before the function fcn finishes running. You can use fetchOutputs to retrieve the results from the future. To stop running the function fcn, use the cancel function. For more information about futures, see Future.

example

[F](#mw%5Fd398454b-bf36-4b2f-969f-bf8ed938afd1) = parfeval([fcn](#mw%5F67bd9e88-7c74-487b-902a-b1096bb2c249),[numFcnOut](#mw%5F6353cac3-5336-4646-a953-f4df69429b5c),[X1,...,Xm](#mw%5F7ea4f1da-203c-41d1-b794-a429c0f3a8fe)) schedules the function fcn to be run.

MATLAB returns the Future object F before the function fcn finishes running.

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

Examples

collapse all

Run Functions in Background

This example shows how to run a function in the background using parfeval and backgroundPool. When you run a function in the background, you can run other MATLAB® code at the same time.

Use parfeval to run the function magic(3) and retrieve one output. 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,@magic,1,3);

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

ans = 3×3

 8     1     6
 3     5     7
 4     9     2

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

collapse all

fcn — Function to run

function handle

Function to run, specified as a function handle.

Example: fcn = @magic

numFcnOut — Number of output arguments requested

nonnegative integer scalar

Number of output arguments requested from function fcn, specified as an nonnegative integer scalar.

numFcnOut is the number of output arguments you request when you run fcn(X1,...,Xm).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

X1,...,Xm — Input arguments

comma-separated list of variables or expressions

Input arguments, specified as a comma-separated list of variables or expressions.

Extended Capabilities

Automatic Parallel Support

Accelerate code by automatically running computation in parallel using Parallel Computing Toolbox™.

Usage notes and limitations:

For more information, see Run MATLAB Functions with Automatic Parallel Support (Parallel Computing Toolbox).

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.