poll - Retrieve data from PollableDataQueue - MATLAB (original) (raw)
Retrieve data from PollableDataQueue
Syntax
Description
[data](#mw%5F05dee558-ad5c-4689-904b-0fe9147da410) = poll([q](#mw%5Fdda4dbb3-95f2-451a-b43b-af3118672d3a))
retrieves one item of data from the PollableDataQueue
object q
. Send data from the background or your current MATLAB® session using send.
You can retrieve data sent from a function that you run on a parallel pool if you have Parallel Computing Toolbox™. For details, see poll (Parallel Computing Toolbox).
- If no data is in the queue,
poll
returns[]
. - If data is in the queue,
poll
returns the oldest item of data in the queue.
[data](#mw%5F05dee558-ad5c-4689-904b-0fe9147da410) = poll([q](#mw%5Fdda4dbb3-95f2-451a-b43b-af3118672d3a),[timeout](#mw%5F8fc9b947-4cc4-4422-8894-73c4c27d693e))
waits timeout
seconds to retrieve an item of data from thePollableDataQueue
object q
.
- If data is in the queue,
poll
returns the oldest item of data in the queue. - 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[]
.
[[data](#mw%5F05dee558-ad5c-4689-904b-0fe9147da410),[tf](#mw%5F84199d1c-f3dc-4347-8407-3040b686be7d)] = poll(___)
tries to retrieve data from a queue and returns a flagtf
. If poll
returns data,tf
is true
. Otherwise, tf
isfalse
.
You can use this syntax with any of the input argument combinations in the previous syntaxes. For example, [data,tf] = poll(q,5)
waits to retrieve data from the queue q
for five seconds.
Examples
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
Input Arguments
q
— Queue
parallel.pool.PollableDataQueue
object
Queue, specified as a parallel.pool.PollableDataQueue
object.
timeout
— Seconds to wait for
real numeric scalar
Seconds to wait for, specified as a real numeric scalar.
Example: timeout = 5;
Example: timeout = single(3.14);
Output Arguments
data
— Data sent to queue
scalar | vector | matrix | multidimensional array
Data sent to the queue, specified as scalar, vector, matrix, or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
| function_handle
| categorical
| datetime
| duration
| calendarDuration
tf
— Flag to specify if data has been returned
true
| false
Flag to specify if data has been returned, returned as true
orfalse
.
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