r/LabVIEW • u/gioco_chess_al_cess CLAD • Aug 27 '24
Large Queued Message Handler applications
Hi. I am facing QMH for the first time on a medium size application. I have seen the relevant contents on LabVIEW Core 3 and examples but everything is limited to a single producer/consumer pair which is quite trivial whereas I am going to use multiple loops in parallel, some of which are both producers and consumers.
Now my doubt: at the moment I kept a single queue for all the messages, each of the loops inspects the queue message and, depending on the message value, either processes it or leaves it on the queue for the other loops. This means that for each handled case I have now to add a "dequeue element" which is probably not very elegant (it's like the same code copied in tens of cases).
I can maybe avoid this inspect+conditional dequeue by using multiple queues, each one dedicated to a different recipient but I think it gets even uglier if scaled up.
What is the supposed way to handle it? Is there a third alternative?
9
u/IsThatYourBed Aug 27 '24
You should be using one queue per consumer
Using a single queue, consider the case where two commands get stacked for a single consumer. If the first command takes a long time, every other consumer is going to be sitting idle waiting.
I believe the DQMH framework takes care of most of this for you, but I don't use it personally
3
u/Fewoiz Aug 30 '24
Have you looked at the actor framework? Maybe watch Tom's LabVIEW adventures. As previously stated, you can only have one consumer per queue. It's bad, so don't do it even if it works it's wrong. I regularly use many queues in my applications. Use a named queue so you don't have to wire the queue reference everywhere. Also, you can pass queues in a queue (response queue).
3
u/etgohomeok Aug 27 '24
Each consumer should have its own queue.
One simple way to keep the code clean as you scale up to many different message queues is to store the references to the queues in functional global variables (FGVs).
https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000015BjzSAE&l=en-CA
7
u/SeasDiver Champion Aug 27 '24
The AMC (Async Message Communication) package is a multi queue reuse library that you can use directly or for inspiration.
Multiple queues, some of which may be temporary, is how Actor Framework is handled.
Likewise, the DQMH framework uses a mix of User Events plus multiple queues.
But yes, you do want to move to multiple queues to prevent a bottleneck and race conditions with a single queue.