Main Content

afterEach

Define a function to call when new data is received on a DataQueue

Description

example

listener = afterEach(queue, funtocall) specifies a function funtocall to execute each time the queue 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, call afterEach 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 calling afterEach, any current data in the queue is dispatched immediately to the supplied function.

Examples

collapse all

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 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
    send(q, 2); 
end
     2
send(q, 3)
     3

You can also first send various messages to the queue. When you call afterEach, the pending messages are passed to the afterEach 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);
     2

     3

Construct a DataQueue and create a listener.

D = parallel.pool.DataQueue;
listener = D.afterEach(@disp);

Send some data with the value 1.

D.send(1)
     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

collapse all

Data queue, specified as a parallel.pool.DataQueue object.

Example: q = parallel.pool.DataQueue;

Function handle, specifying the function added to the list of functions to call when a piece of new data is received from queue.

Example: listener = afterEach(queue, funtocall)

All callback functions must accept data as single argument.

afterEach(queue, @foo) expects a function handle @foo to a function of the form

function foo(data)
end 
When send(queue, someData) is called on the worker, someData is serialized and sent back to the client. someData is deserialized on the client and passed as the input to foo(data).

Output Arguments

collapse all

Listener object created by afterEach, returned as the handle to an event.listener object.

Version History

Introduced in R2017a