06-27-2018 01:27 AM
Hi all,
In my App I've got 4 subVIs running in parallel with while loops inside. Can you tell me the best way to communicate between them? For example a while loop in subVI_1 has to process the data from the while loop in subVI_2. Or while loop in main VI has to pass information to all subVIs to stop while loops an terminate operation.
My current solution is:
- in main VI I initialize a cluster with all data and control signals
- I pass to subVIs Reference of this cluster
- in subVIs I read / write to the cluster using Property Node and bundle/unbundle
I've tried ques but this solution seems to be cleaner for me. Can I do it better?
06-27-2018 01:41 AM
reading/writing property nodes sometimes are slow, using queues are better.
Read/write pr.node only when something changed, i mean
06-27-2018 01:59 AM - edited 06-27-2018 02:11 AM
Hi Tomasz,
For example a while loop in subVI_1 has to process the data from the while loop in subVI_2.
Use a queue.
Think Producer-Consumer structure…
Or while loop in main VI has to pass information to all subVIs to stop while loops an terminate operation.
Use a notifier.
Or a global variable. (In WORM aka "write once read many" mode.)
Or stop the subVIs when the producer queue is killed…
06-27-2018 02:00 AM
Hi,
basically it seems as though you're using the cluster as a global variable and passing references to subvis to modifify the global variable. If you want to use a global variable, just do that, no need for references.
That being said, global variables are usually a bad thing. Funny things start to happen when you read/write from different locations in your code, like race conditions. For passing data between loops, I would suggest queues, or channels. Evaluate them, they are made for this kinda thing.
For your termination, I've always used a notifier. I encapsulated this in an api which had 4 subvis (init, read termination notifier, write termination notifier, close). This way, you have the notifier code centralised and can also react to any errors that might be passed.
Depending on your requirements, there isn't a one-stop answer. What I do know is that global variables tend to be a bad thing if not used properly.
06-27-2018 02:22 AM - edited 06-27-2018 02:36 AM
here's the example.
Yes it looks like a Global Variable but it's much faster (I've tested the performance)
The thing is that there's no an easy classification producer - customer. In target applications each while loop can produce something and be a customer for a data. So it need to access something and it need to be able to write its' own data. I'm passing the "terminate" information in cluster since it's already there and it costs me nothing to do it.
06-27-2018 03:16 AM
Hi Tomasz,
The thing is that there's no an easy classification producer - customer. In target applications each while loop can produce something and be a customer for a data.
This leads to a discussion about using the proper code architecture…
So it need to access something and it need to be able to write its' own data.
What about CVT (current value tables) aka TagBus aka VariantAttributes implementations?
06-27-2018 04:29 AM
@TomaszCiesla wrote:
here's the example.
Yes it looks like a Global Variable but it's much faster (I've tested the performance)
I would really like to see that benchmark code. Every time I benchmark with property nodes, they are 1000s of times slower than local variables and global variables.
As far as what I do, I use a Queue or User Event to pass data and messages to my loops. I set them up as Queued Message Handlers. Included in those messages is the stop message. I find it best to limit the types of communications to 1.
06-27-2018 04:56 AM
We've got a measurement setup in the lab and idea is to control / read each equipment as a separate subVI in independent while loops.Hardware works with frequencies 0.1...2 Hz so the performance is not an issue.
So how can I convert my example to Queues?
06-27-2018 06:27 AM
That references.vi code is riddled with race conditions galore. Your second loop can very easily overwrite the data that loop 1 just wrote and visa versa. In fact, I see a scenario that you will not be able to stop 2 of your 3 loops because of this race condition. Have a look at this document I made: A Look At Race Conditions. I did not cover the property nodes, but it will follow the same concepts as the local variable except A LOT slower. I might just go ahead an update that benchmark to include the property nodes when I feel like going for a long walk since it will take forever.
So let's boil down to what you actually need here. Do you need your main loop to handle ALL of the data or just the latest data at that moment in time? This is a very important requirement to define.
If you only care about the latest data, then we are dealing with "Tag" data. Ways to pass Tag data for only 1 writer are Global Variables or Tag Channel Wires. If you are doing a Read-Modify-Write on the data in multiple places, then you need to look at an Action Engine.
If you need to process all of the data (for example, logging), then you really need to look at the Queued Message Handler. Use a single queue. In the queue data type should be a string (or enum) to state what the data is and a variant to hold the actual data. The QMH can then handle the data however it needs to.
Your stop condition is the perfect example of a Tag. So I would just use a Global Variable. Alternatively, you can use a Notifier which will double as your wait function when using the timeout.
06-27-2018 06:51 AM
I need logging. So let's got into the details a bit. Let's say that I've got 2 devices: pump and climate chamber. I need to be able to log the data from both and give them a setpoint (temperature, flow). Since there are more devices in total preferable architecture are parallel while loops.
- The device's while loop puts one the queue its' registered value (time + value) as string
- The main while loop reads the queue, decodes the data and builds tables for logging from it
- The main while loop communicates stop signal and setpoints using global variables
so in this scenario I abandon bidirectional approach - the device's while loop can only stream the data to mail while loop. And main while loop communicates with device's while loop using the global variables