DataQueue - Send and automatically process data - MATLAB (original) (raw)

Main Content

parallel.pool.DataQueue

Send and automatically process data

Description

Use a DataQueue object to automatically process data sent from a function that you run in the background or in your current MATLAB® session.

You can automatically process data sent from a function that you run on a parallel pool if you have Parallel Computing Toolbox™. For details, see parallel.pool.DataQueue (Parallel Computing Toolbox).

When you create a DataQueue object, you create a connection to the current MATLAB session that you can use to send and receive messages.

Creation

Syntax

Description

`q` = parallel.pool.DataQueue creates an object that can be used to send and automatically process messages.

example

Properties

expand all

QueueLength — Number of items waiting to be removed from queue

zero or positive integer

This property is read-only.

Number of items of data waiting to be removed from the queue, specified as a zero or positive integer.

For example, if you create a DataQueue object in the current MATLAB session, you can run a function in the background with thatDataQueue object as an input argument. TheQueueLength property of that DataQueue object will always be 0 in the background.

Object Functions

afterEach Run function after data is received on DataQueue
send Send data to DataQueue orPollableDataQueue

Examples

collapse all

Automatically Process Data Sent from the Background

This example shows how to automatically process data in your current MATLAB session that you send from the background.

Create a DataQueue object. After each item of data is received on the DataQueue in your current MATLAB session, automatically display that item using the disp function.

q = parallel.pool.DataQueue; afterEach(q,@disp);

The helper function magicWithSend defined at the end of this example sends the sum of a magic square to a DataQueue or PollableDataQueue object, then returns that magic square.

Use parfeval and backgroundPool to run the function magicWithSend in the background.

f = parfeval(backgroundPool,@magicWithSend,1,q,3);

The sum is displayed before you fetch outputs from the future. To retrieve the output from the background, use fetchOutputs. MATLAB returns the output once the execution of magicWithSend is complete.

ans = 3×3

 8     1     6
 3     5     7
 4     9     2

Define Helper Function

Define the helper function magicWithSend. The function creates a magic square, then sends the sum of the magic square to a DataQueue or PollableDataQueue object. After the sum is sent, the function returns the magic square.

function X = magicWithSend(q,n) X = magic(n); s = sum(X,'all'); send(q,s); end

Extended Capabilities

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.

Version History

Introduced in R2017a