Main Content

backgroundPool

Environment for running code in the background

    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

    Description

    example

    p = backgroundPool returns the background pool.

    Properties

    expand all

    This property is read-only.

    Number of workers, specified as a positive integer scalar.

    • If you do not have Parallel Computing Toolbox™, the value is 1.

    • If you have Parallel Computing Toolbox, the value is between 1 and 8. If you have 8 or fewer physical cores on your machine, NumWorkers is equal to the number of physical cores you have. If you have more than 8 physical cores on your machine, NumWorkers is 8. For example, if you run MATLAB on a machine with four physical cores, the value is 4.

    Object Functions

    parfevalRun function in background
    parfevalOnAll (Parallel Computing Toolbox)Execute function asynchronously on all workers in parallel pool

    Examples

    collapse all

    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.

    fetchOutputs(f)
    ans = 3×3
    
         8     1     6
         3     5     7
         4     9     2
    
    

    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)

    See Also

    | (Parallel Computing Toolbox)

    Introduced in R2021b