BackgroundPool - Environment for running code in the background - MATLAB (original) (raw)

Environment for running code in the background

Since R2021b

Description

Use the background pool to run code in the background. When you run code in the background, you can run other code in your MATLAB® session at the same time.

Creation

Syntax

Description

`p` = backgroundPool returns the background pool.

example

Properties

expand all

FevalQueue — Queue of FevalFuture objects to run on the background pool

FevalQueue object

This property is read-only.

Queue of FevalFuture objects to run on the background pool, specified as an FevalQueue object. You can use this property to check the pending and running future variables of the parallel pool. To create future variables, use parfeval (Parallel Computing Toolbox) and parfevalOnAll (Parallel Computing Toolbox). For more information on future variables, see Future.

Data Types: FevalQueue

NumWorkers — Number of workers

positive integer scalar

This property is read-only.

Number of workers, specified as a positive integer scalar.

Busy — Flag that indicates whether the background pool is busy

true | false

This property is read-only.

Flag that indicates whether the background pool is busy, specified astrue or false. The pool is busy if there is outstanding work for the pool to complete.

Object Functions

parfeval Run function in background
parfevalOnAll (Parallel Computing Toolbox) Run function asynchronously on all workers in parallel pool

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

Read Images from S3 Bucket in Background

This example shows how to read images from an Amazon® S3 bucket in the background.

Use an image datastore to connect to your bucket. For more information on how to set up access to your S3 bucket, see Work with Remote Data.

Replace "s3://MyBucket/data" with a URL to a data folder in your S3 bucket.

ds = imageDatastore("s3://MyBucket/data");

Use parfeval to read data from the S3 bucket. Specify backgroundPool as the first argument to run the function in the background. Then, read all of the data from the datastore.

f = parfeval(backgroundPool,@readall,1,ds);

You can run other functions while you download the data from the S3 bucket. To retrieve all of the images from the background, use fetchOutputs. MATLAB returns the output once the execution of readall is complete.

T = fetchOutputs(f); imout = imtile(T); imshow(imout)

Limitations

Extended Capabilities

Thread-Based Environment

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

Usage notes and limitations:

For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced in R2021b