afterEach - Define a function to call when new data is received on a data queue - MATLAB (original) (raw)
Define a function to call when new data is received on a data queue
Syntax
Description
[listener](#bvmii2l-listener) = afterEach([queue](#bvmii2l-queue),[funtocall](#bvmii2l-funtocall))
specifies a function funtocall
to execute each time thequeue
receives new data. You can specify multiple different functions to call, because each call to afterEach
creates a new listener on the queue. If you want to specify another function, callafterEach
again. To remove the registration of the function with the queue
, delete the returned listener
object.
You must call afterEach
in the same process where you created the data queue, otherwise an error occurs. After callingafterEach
, any current data in the queue is immediately passed to the specified function.
Examples
Call afterEach
to Dispatch Data on a Queue
If you call afterEach
and there are items on the queue waiting to be dispatched, these items are immediately dispatched to the afterEach
function. Call afterEach
before sending data to the queue, to ensure that on send
, the function handle specified by afterEach
is called.
Construct a DataQueue
and callafterEach
.
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 send(q, 2); end
You can also first send various messages to the queue. When you callafterEach
, the pending messages are passed to theafterEach
function, in this example to the function handle @disp
.
q = parallel.pool.DataQueue; parfor i = 1 send(q, 2); end send(q, 3)
afterEach(q, @disp);
Remove a Callback by Deleting the Listener
Construct a DataQueue
and create a listener.
D = parallel.pool.DataQueue; listener = D.afterEach(@disp);
Send some data with the value 1.
Delete the listener.
delete(listener) D.send(1)
No data is returned because you have removed the callback by deleting the listener.
Input Arguments
queue
— Data queue
parallel.pool.DataQueue
Data queue, specified as a parallel.pool.DataQueue
object.
Example: q = parallel.pool.DataQueue;
funtocall
— Callback function
function handle
Callback function added to the list of functions to call when new data is received from queue
, specified as a function handle.
All callback functions must accept data
as single argument.
afterEach(queue,@foo)
expects a function handle@foo
to a function of the form
When you call send(queue,someData)
, the data queue serializes someData
and sends it back to the client. On the client, the data queue de-serializessomeData
and passes it as the input tofoo(data)
.
Example: listener = afterEach(queue,funtocall)
Output Arguments
listener
— listener
event.listener
Listener object created by afterEach
, returned as the handle to an event.listener object.
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