Main Content

close

Close pollable data queue

Since R2025a

    Description

    close(pollablequeue) closes the parallel.pool.PollableDataQueue object specified by pollablequeue. You can no longer send data to the queue using the send function.

    example

    Examples

    collapse all

    Define a function, sendMessages, that sends messages to a pollable data queue. It sends five messages, simulating work by pausing for one second between sending each message.

    function sendMessages(queue)
        for idx = 1:5
            send(queue,sprintf("Message %d from worker",idx));
            pause(1);
        end
    end

    Create a PollableDataQueue object, and use parfeval to execute the sendMessages function in the background. Pause briefly to allow the worker to send some messages.

    queue = parallel.pool.PollableDataQueue;
    f = parfeval(backgroundPool,@sendMessages,0,queue);
    pause(2);

    To stop the background from sending any more data to the queue, close the PollableDataQueue object.

    close(queue);

    Wait for the parfeval computation to complete. Query the Error property of future object f to verify that an error occurred due to an attempt to send messages to the closed queue.

    wait(f);
    f.Error.message
    ans = 
    'Failed to send data because the DataQueue has been closed.'
    

    Input Arguments

    collapse all

    Pollable data queue, specified as a PollableDataQueue object.

    After you close a PollableDataQueue object, you can no longer send data to the queue. Any attempt to send data to the queue results in an error. You can continue to poll the queue for data. You cannot reopen a closed queue.

    Example: p = parallel.pool.PollableDataQueue;

    Extended Capabilities

    expand all

    Version History

    Introduced in R2025a