# Device Queue Event

This example shows how to use `getQueueEvent` function in order to be notified when one of the packets from selected streams
arrive

## Demo

## Setup

Please run the [install script](https://github.com/luxonis/depthai-python/blob/main/examples/install_requirements.py) to download
all required dependencies. Please note that this script must be ran from git context, so you have to download the
[depthai-python](https://github.com/luxonis/depthai-python) repository first and then run the script

```bash
git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py
```

For additional information, please follow the [installation guide](https://docs.luxonis.com/software/depthai/manual-install.md).

## Source code

#### Python

```python
#!/usr/bin/env python3

"""
 This example demonstrates use of queue events to block a thread until a message
 arrives to any (of the specified) queue
"""

import cv2
import depthai as dai

# Create pipeline
pipeline = dai.Pipeline()

# Define sources and outputs
camRgb = pipeline.create(dai.node.ColorCamera)
camMono = pipeline.create(dai.node.MonoCamera)
xoutRgb = pipeline.create(dai.node.XLinkOut)
xoutMono = pipeline.create(dai.node.XLinkOut)

xoutRgb.setStreamName("rgb")
xoutMono.setStreamName("mono")

# Properties
camRgb.setInterleaved(True)
camRgb.setPreviewSize(300, 300)

# Linking
camRgb.preview.link(xoutRgb.input)
camMono.out.link(xoutMono.input)

# Connect to device and start pipeline
with dai.Device(pipeline) as device:

    # Clear queue events
    device.getQueueEvents()

    while True:
        # Block until a message arrives to any of the specified queues
        queueName = device.getQueueEvent(("rgb", "mono"))

        # Getting that message from queue with name specified by the event
        # Note: number of events doesn't necessarily match number of messages in queues
        # because queues can be set to non-blocking (overwriting) behavior
        message = device.getOutputQueue(queueName).get()

        # Display arrived frames
        if type(message) == dai.ImgFrame:
            cv2.imshow(queueName, message.getCvFrame())

        if cv2.waitKey(1) == ord('q'):
            break
```

#### C++

```cpp
#include <iostream>

// Includes common necessary includes for development using depthai library
#include "depthai/depthai.hpp"

int main() {
    // Create pipeline
    dai::Pipeline pipeline;

    // Define sources and outputs
    auto camRgb = pipeline.create<dai::node::ColorCamera>();
    auto camMono = pipeline.create<dai::node::MonoCamera>();
    auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
    auto xoutMono = pipeline.create<dai::node::XLinkOut>();

    xoutRgb->setStreamName("rgb");
    xoutMono->setStreamName("mono");

    // Properties
    camRgb->setInterleaved(true);
    camRgb->setPreviewSize(300, 300);

    // Linking
    camRgb->preview.link(xoutRgb->input);
    camMono->out.link(xoutMono->input);

    // Connect to device and start pipeline
    dai::Device device(pipeline);

    // Clear queue events
    device.getQueueEvents();

    while(true) {
        auto ev = device.getQueueEvent();

        if(ev == "rgb") {
            auto rgb = device.getOutputQueue(ev)->get<dai::ImgFrame>();
            cv::imshow("rgb", rgb->getFrame());
        } else if(ev == "mono") {
            auto mono = device.getOutputQueue(ev)->get<dai::ImgFrame>();
            cv::imshow("mono", mono->getFrame());
        }

        int key = cv::waitKey(1);
        if(key == 'q' || key == 'Q') {
            return 0;
        }
    }
    return 0;
}
```

## Pipeline

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
