This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

DataQueue

Class that enables sending and listening for data between client and workers

Properties

expand all

Dependent property on the queue that indicates how many items of data are waiting to be removed from the queue.

q = parallel.pool.DataQueue;
% No messages in queue because nothing has been sent.
q.QueueLength
ans =

     0
q.send('sending a message')
% Now QueueLength = 1 because one message has been sent.
q.QueueLength
ans =

     1
% Add a callback to process the queue.
listener = q.afterEach(@disp);
sending a message
% Now QueueLength = 0 because there are no more pending messages.
q.QueueLength
ans =

     0
% Data sent now is immediately processed by the callback so that QueueLength remains 0.
q.send('sending message 2')
q.QueueLength
sending message 2

ans =

     0
% Deleting all callback listeners causes messages to build up in the queue again.
delete(listener)
q.send('sending message 3')
q.QueueLength
ans =

     1

Examples

collapse all

Construct a DataQueue, and call afterEach.

q = parallel.pool.DataQueue;
afterEach(q, @disp);
Start a parfor-loop, and send a message. The pending message is passed to the afterEach function, in this example @disp.

parfor i = 1:3
    send(q, i); 
end;
     1

     2

     3

For more details on listening for data using a DataQueue, see afterEach.

Create a DataQueue, and use afterEach to specify the function to execute each time the queue receives data. This example calls a subfunction that updates the wait bar.

Create a parfor-loop to carry out a computationally demanding task in MATLAB®. Use send to send some dummy data on each iteration of the parfor-loop. When the queue receives the data, afterEach calls nUpdateWaitbar in the client MATLAB, and you can observe the wait bar progress.

function a = parforWaitbar

D = parallel.pool.DataQueue;
h = waitbar(0, 'Please wait ...');
afterEach(D, @nUpdateWaitbar);

N = 200;
p = 1;

parfor i = 1:N
    a(i) = max(abs(eig(rand(400))));
    send(D, i);
end

    function nUpdateWaitbar(~)
        waitbar(p/N, h);
        p = p + 1;
    end
end

If you call afterEach and there are items on the queue waiting to be dispatched, these items are immediately dispatched to the function handle specified by afterEach. Call afterEach before sending data to the queue, to ensure that on send, the function handle @disp is called.

Construct a DataQueue and call afterEach.

q = parallel.pool.DataQueue;
afterEach(q, @disp);
If you then send messages to the queue, each message is passed to the function handle specified by afterEach immediately.

parfor i = 1:3
    send(q, i); 
end
send(q, 0);
     1

     3

     2

     0

If you send the data to the queue and then call afterEach, each of the pending messages are passed to the function handle specified by afterEach.

q = parallel.pool.DataQueue;
parfor i = 1:3
    send(q, i); 
end
afterEach(q, @disp);
       3

       1

       2

Introduced in R2017a