Main Content

parallel.pool.PollableDataQueue

Send and manually retrieve data

    Description

    Use a PollableDataQueue object to send data from a function that you run in the background, on a parallel pool, or in your current MATLAB® session.

    You can manually retrieve data sent from a function that you run on a parallel pool if you have Parallel Computing Toolbox™.

    When you create a PollableDataQueue object, you create a connection to the current MATLAB session that you can use to send and receive messages.

    • To send data to the current MATLAB session, use send.

    • To manually retrieve data after it is received in the current MATLAB session, use poll.

    Creation

    Description

    example

    q = parallel.pool.PollableDataQueue creates an object that can be used to send and manually retrieve messages.

    Properties

    expand all

    This property is read-only.

    Number of items of data waiting to be removed from the queue, specified as a zero or positive integer.

    • The value is a zero or positive integer in the MATLAB session that creates the PollableDataQueue object.

    • Everywhere else, the value is 0.

    For example, if you create a PollableDataQueue object in the current MATLAB session, you can run a function in the background with that PollableDataQueue object as an input argument. The QueueLength property of that PollableDataQueue object will always be 0 in the background.

    Object Functions

    pollRetrieve data from PollableDataQueue
    sendSend data to DataQueue or PollableDataQueue

    Examples

    collapse all

    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.

    fetchOutputs(f)
    ans = 3×3
    
         8     1     6
         3     5     7
         4     9     2
    
    

    Use the poll function to collect data from the queue.

    poll(q)
    ans = 45
    

    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

    See Also

    | | | | (Parallel Computing Toolbox) | | (Parallel Computing Toolbox) | | (Parallel Computing Toolbox) | (Parallel Computing Toolbox)

    Introduced in R2017a