Skip to content

Commit 7950bdc

Browse files
authored
Merge pull request #292 from luxonis/queue_docs_path
refactored queue docs a bit, added additional info
2 parents 5d654a5 + 18a3fd5 commit 7950bdc

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

docs/source/components/device.rst

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,58 @@ If you want to use multiple devices on a host, check :ref:`Multiple DepthAI per
4040
Device queues
4141
#############
4242

43-
After initializing the device, one has to initialize the input/output queues as well. You can create an input queue with
44-
:code:`device.getInputQueue("input_name")` and output queue with :code:`device.getOutputQueue("output_name")`.
45-
46-
47-
When you define an output queue, the device can write to it at any point in time, and the host can read from it at any point in time.
48-
There might be a cases when the host is reading very fast from the queue, and the queue, no matter its size, will stay empty most of
49-
the time. But as we add things on the host side (additional processing, analysis etc), it may happen that the device will be writing to
50-
the queue faster than the host can read from it. And then the packets in the queue will start to add up - and both maxSize and blocking
51-
flags determine the behavior of the queue in this case.
52-
53-
By default, the queue is blocking and its size is 30, so the device will put
54-
30 packets at most, and when the limit is reached, it will hang on queue put call and wait until it can successfully complete this
55-
call (so, waits for the host to consume the packet before putting a new one). Making the queue non-blocking will change its behavior
56-
in this situation - instead of waiting, it will discard the oldest packet and add the new one, and then continue it's processing
57-
loop (so it won't get blocked). maxSize determines the size of the queue and also
58-
helps to control the memory usage - if the packet have 5MB of data, and the queue size is 30, this queue effectively stores
59-
150MB of data in memory (the packets can also get really big, for instance a single 4K NV12 encoded frame takes about ~12MB).
60-
Decreasing the queue size to 1 and setting non-blocking behavior will effectively mean "I only want the latest packet from the queue".
61-
62-
The size and behavior of the queue can be modified after the initialization by calling :code:`queue.setBlocking()` and :code:`queue.setMaxSize()`.
43+
After initializing the device, one has to initialize the input/output queues as well.
44+
45+
.. code-block:: python
46+
47+
outputQueue = device.getOutputQueue("output_name")
48+
inputQueue = device.getInputQueue("input_name")
49+
50+
When you define an output queue, the device can push new messages to it at any point in time, and the host can read from it at any point in time.
51+
Usually, when the host is reading very fast from the queue, the queue (regardless of its size) will stay empty most of
52+
the time. But as we add things on the host side (additional processing, analysis, etc), it may happen that the device will be writing to
53+
the queue faster than the host can read from it. And then the messages in the queue will start to add up - and both maxSize and blocking
54+
flags determine the behavior of the queue in this case. You can set these flags with:
55+
56+
.. code-block:: python
57+
58+
# When initializing the queue
59+
queue = device.getOutputQueue(name="name", maxSize=5, blocking=False)
60+
61+
# Or afterwards
62+
queue.setMaxSize(10)
63+
queue.setBlocking(True)
64+
65+
Blocking behaviour
66+
******************
67+
68+
By default, queues are **blocking** and their size is **30**, so when the device fills up a queue and when the limit is
69+
reached, any additional messages from the device will be blocked and the library will wait until it can add new messages to the queue.
70+
It will wait for the host to consume (eg. :code:`queue.get()`) a message before putting a new one into the queue.
71+
72+
.. note::
73+
After the host queue gets filled up, the XLinkOut.input queue on the device will start filling up. If that queue is
74+
set to blocking, other nodes that are sending messages to it will have to wait as well. This is a usual cause for a
75+
blocked pipeline, where one of the queues isn't emptied in timely manner and the rest of the pipeline waits for it
76+
to be empty again.
77+
78+
Non-Blocking behaviour
79+
**********************
80+
Making the queue non-blocking will change the behavior in the situation described above - instead of waiting, the library will discard
81+
the oldest message and add the new one to the queue, and then continue its processing loop (so it won't get blocked).
82+
:code:`maxSize` determines the size of the queue and it also helps to control memory usage.
83+
84+
For example, if a message has 5MB of data, and the queue size is 30, this queue can effectively store
85+
up to 150MB of data in the memory on the host (the messages can also get really big, for instance, a single 4K NV12 encoded frame takes about ~12MB).
86+
87+
Some additional information
88+
***************************
89+
90+
- Decreasing the queue size to 1 and setting non-blocking behavior will effectively mean "I only want the latest packet from the queue".
91+
- Queues are thread-safe - they can be accessed from any thread.
92+
- Queues are created such that each queue is its own thread which takes care of receiving, serializing/deserializing, and sending the messages forward (same for input/output queues).
93+
- The :code:`Device` object isn't fully thread-safe. Some RPC calls (eg. :code:`getLogLevel`, :code:`setLogLevel`, :code:`getDdrMemoryUsage`) will get thread-safe once the mutex is set in place (right now there could be races).
94+
6395

6496
Reference
6597
#########

0 commit comments

Comments
 (0)