PollableDataQueue - Send and manually retrieve data - MATLAB (original) (raw)

Main Content

parallel.pool.PollableDataQueue

Send and manually retrieve data

Description

Use a PollableDataQueue object to send data from a function that you run in the background or in your current MATLAB® session.

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

When you create a PollableDataQueue 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.PollableDataQueue creates an object that can be used to send and manually retrieve 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 PollableDataQueue object in the current MATLAB session, you can run a function in the background with thatPollableDataQueue object as an input argument. TheQueueLength property of that PollableDataQueue object will always be 0 in the background.

Object Functions

poll Retrieve data from PollableDataQueue
send Send data to DataQueue orPollableDataQueue

Examples

collapse all

Manually Retrieve Data Sent from the Background

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

Create a PollableDataQueue object.

q = parallel.pool.PollableDataQueue;

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);

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

Use the poll function to collect data from the queue.

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