Main Content

afterEach

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

Description

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 immediately passed to the specified function.

example

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;

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

function foo(data)
end 
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-serializes someData and passes it as the input to foo(data).

Example: listener = afterEach(queue,funtocall)

Output Arguments

collapse all

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

Extended Capabilities

Version History

Introduced in R2017a