Main Content

poll

Retrieve data sent from a worker

Description

example

poll(pollablequeue) retrieves the result of a message or data sent from a worker to the parallel.pool.PollableDataQueue specified by pollablequeue. You can use poll only in the process where you created the data queue.

example

[data, OK] = poll(pollablequeue, timeout) returns data, and OK as a boolean true to indicate that data has been returned. If there is no data in the queue, then an empty array is returned with a boolean false for OK. Specify timeout in seconds as an optional second parameter. In that case, the method might block for the time specified by timeout before returning. If any data arrives in the queue during that period, that data is returned.

Examples

collapse all

Construct a PollableDataQueue.

p = parallel.pool.PollableDataQueue;
Start a parfor-loop, and send a message, such as data with the value 1.
parfor i = 1
    send(p, i); 
end
Poll for the result.

poll(p)
1

For more details on sending data using a PollableDataQueue, see send.

This example shows how to return intermediate results from a worker to the client and to display the result on the client.

Construct a PollableDataQueue. A PollableDataQueue is most useful for sending and polling for data during asynchronous function evaluations using parfeval or parfevalOnAll.

q = parallel.pool.PollableDataQueue;
Start a timer and send the data queue as input to the function for parfeval execution on the pool. Display the time elapsed and the data returned.

f = parfeval(@workerFcn, 0, q);
msgsReceived = 0;
starttime = tic;
while msgsReceived < 2
    [data, gotMsg] = poll(q, 1);
    if gotMsg
        fprintf('Got message: %s after %.3g seconds\n', ...
            data, toc(starttime));
        msgsReceived = msgsReceived + 1;
    else
        fprintf('No message available at %.3g seconds\n', ...
            toc(starttime));
    end
end

function workerFcn(q)
    send(q, 'start');
    pause(3);
    send(q, 'stop');
end
Got message: start after 0.39 seconds
No message available at 1.48 seconds
No message available at 2.56 seconds
Got message: stop after 3.35 seconds

The first message is returned in 0.39 s after you have executed parfeval. In that time the data and function for parfeval have been serialized, sent over to the workers, deserialized and set running. When you start the code, the worker sends some data, which is serialized, sent over the network back to the client and put on a data queue. poll notes this operation and returns the value to the client function. Then the time taken since parfeval was called is displayed. Note a delay of 3 s while the worker is computing something (in this case a long pause).

Input Arguments

collapse all

Pollable data queue, specified as a parallel.pool.PollableDataQueue object.

Example: [data, OK] = poll(pollablequeue, optionalTimeout);

Optional timeout interval (in seconds) used to block poll before returning, specified as a scalar.

Example: [data, OK] = poll(pollablequeue, timeout);

Output Arguments

collapse all

Message or data from workers to a data queue, specified as any serializable value.

Example: [data, OK] = poll(pollablequeue, timeout);

Check if data has been returned, returned as a Boolean value. If data has been returned, then OK is assigned the value of a boolean true. If there is no data in the queue pollablequeue, then an empty array is returned and a boolean false for OK.

Example: [data, OK] = poll(pollablequeue, timeout);

Extended Capabilities

Version History

Introduced in R2017a