Main Content

parallel.pool.DataQueue

Send and automatically process data

    Description

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

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

    When you create a DataQueue 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 automatically process data when it is received in the current MATLAB session, use afterEach.

    Creation

    Description

    example

    q = parallel.pool.DataQueue creates an object that can be used to send and automatically process 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 DataQueue object.

    • Everywhere else, the value is 0.

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

    Object Functions

    afterEachRun function after data is received on DataQueue
    sendSend data to DataQueue or PollableDataQueue

    Examples

    collapse all

    This example shows how to automatically process data in your current MATLAB session that you send from the background.

    Create a DataQueue object. After each item of data is received on the DataQueue in your current MATLAB session, automatically display that item using the disp function.

    q = parallel.pool.DataQueue;
    afterEach(q,@disp);

    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);

    The sum is displayed before you fetch outputs from the future. 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
    
    

    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

    Extended Capabilities

    Version History

    Introduced in R2017a

    See Also

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