poll - Retrieve data sent to pollable data queue - MATLAB (original) (raw)
Retrieve data sent to pollable data queue
Syntax
Description
[data](#bvl%5Fs7o-data) = poll([pollablequeue](#bvl%5Fs7o-pollablequeue))
retrieves one item of data from the parallel.pool.PollableDataQueue
object specified by pollablequeue
.
- If data is in the queue,
poll
returns the oldest item of data in the queue, even if the queue is closed. - If no data is in the queue,
poll
returns[]
. - If the queue is closed and no data is in the queue,
poll
returns[]
.
[data](#bvl%5Fs7o-data) = poll([pollablequeue](#bvl%5Fs7o-pollablequeue),[timeout](#bvl%5Fs7o-timeout))
waits timeout
seconds to retrieve data from thePollableDataQueue
object pollablequeue
.
- If data is in the queue,
poll
returns the oldest item of data in the queue, even if the queue is closed. - If no data is in the queue,
poll
waits up totimeout
seconds. If the queue receives data beforetimeout
seconds elapse,poll
returns that item. If no data is received in the queue beforetimeout
seconds elapse,poll
returns[]
. - If the queue is closed or is closed during timeout and no data is in the queue,
poll
does not wait and returns[]
.
[[data](#bvl%5Fs7o-data),[tf](#bvl%5Fs7o-OK)] = poll(___)
tries to retrieve data
from the queue. Ifpoll
returns data, tf
is true.
You can use this syntax with any of the input argument combinations in the previous syntaxes. For example, [data,tf] = poll(pollablequeue,5)
waits to retrieve data from the queue pollablequeue for five seconds.
Examples
Create a PollableDataQueue
object.
p = parallel.pool.PollableDataQueue;
Run a parfor
-loop, and send a message, such as data with the value 1.
parfor idx = 1 send(p,idx); end
Poll for the result.
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
Pollable data queue, specified as a PollableDataQueue object.
The destination behavior of the queue, set using theDestination argument of theparallel.pool.PollableDataQueue
function, determines where you can poll for data:
- If you create a PollableDataQueue object without setting the
Destination
argument, or setDestination
to"creator"
, only the client or worker that creates the queue can poll it to receive data. - If you set
Destination
to"any"
, the client or any worker can poll the queue to receive data. The data waits in the queue and is sent to whichever client or worker polls the queue.
Before R2025a: You must callpoll
on the client or worker in which you created the pollable data queue.
If you close aPollableDataQueue
using the close function, you can no longer send data to the queue but you can continue poll for data in the queue. (since R2025a)
Example: data = poll(pollablequeue);
Optional timeout interval (in seconds) used to block poll
before returning, specified as a scalar.
Example: data = poll(pollablequeue,timeout);
Output Arguments
Message or data sent to a data queue, returned as any serializable value.
Example: data = poll(pollablequeue);
Flag to specify if data has been returned, returned as a logicaltrue
or false
.
Example: [data,tf] = poll(pollablequeue,timeout);
Data Types: logical
Extended Capabilities
Version History
Introduced in R2017a