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.
Properties
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.
- If you do not have a license for Parallel Computing Toolbox™, the value is
1
. - If you have a license for Parallel Computing Toolbox,
NumWorkers
is equal to the number of physical cores you have. For example, if you run MATLAB on a machine with four physical cores, the value is4
. You can reduce this value usingmaxNumCompThreads before first usage ofbackgroundPool
.
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
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
- Pools created using
parpool(‘Threads’)
and backgroundPool are both thread-based pools which utilize the same resources. It is possible that activity on one pool may block activity on the other and vice versa. Additionally, persistent data and random number generation stream state are shared in between these pools. For more information on controlling random number streams, see Control Random Number Streams on Workers (Parallel Computing Toolbox).
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:
- Calling
backgroundPool
on thread-workers returns an emptybackgroundPool
object. Any code you run in the background of a thread-worker is evaluated serially.
For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced in R2021b