Actor Framework Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Producer Consumer across actor

I have an actor framework project that produces data that needs saving. Saing can be slower than production so that is an ideal case for Producer-Consumer.

I tried having the producer and the consumer each inside a different actor core but I am having issues getting the behaviour I want.

Basically, I would want the consumer actor to only stop after having "consumed" all data in the queue so that there is no data loss. But because I created the "Obtain queue" in the producer, as soon as the producer exits (without calling "release queue") I still loose the queue reference in the consumer.

Is this just not allowed? Am I doing it wrong? please help.

Download All
0 Kudos
Message 1 of 13
(6,132 Views)

This is kind of wanted behaviour, since a queue will be invalid if the VI setting it up goes out of memory. This is the reason why enqueuers of Actors are created by their respective owner.

If you want to have a dedicated producer/consumer queue, make the consumer set it up and send the Q reference to the producer.

0 Kudos
Message 2 of 13
(5,257 Views)

I looked at your images and I have a couple of comments.

1. Send the data in a message to the consumer. Don't use your own queues. Just stick with the actor messaging.

2. Launch both the producer and the consumer as nested actors under some launcher actor.

3. When you need to stop you can first stop the producer (immediately if you want) and then wait until the consumer is done before stopping it. You will need to implement your own stopping sequence and not use the default stopping.

Casey

Casey Lamers


Phoenix, LLC


casey.lamers@phoenixwi.com


CLA, LabVIEW Champion


Check Out the Software Engineering Processes, Architecture, and Design track at NIWeek. 2018 I guarantee you will learn things you can use daily! I will be presenting!

Message 3 of 13
(5,257 Views)

The Actor Framework already gives you a producer - consumer relationship without having to build your own queues. That is the point of the AF messaging system. The consumer should define a method for saving data. It's sole purpose in life is to consume data and log it to disk. It will have an associated AF Message. The producer, as it produces data, simply calls the "log data" message. No extra queues. When the producer stops producing and closes, the consumer will still operate and chew through it's remaining "log data" message backlog. Assuming the consumer is a nested actor of the producer, it will close on 2 conditions: the producer has stopped (thereby sending a stop message to the consumer) and the consumer has emptied it's message queue.

0 Kudos
Message 4 of 13
(5,257 Views)

Full ACK. To implement a producer consumer design pattern on top of actor framework seems redundant to me too, because everything that is needed (and much more) is already available. This is -in my humble opinion- an anti pattern.

In addition sharing a queue across actors means that you have to share private information. This is something you shouldn't ever do.

To use the actor framework gives also advantages of priority queues:

For example, the consumer empties its message queue automatically, if the stop message has been send with normal priority, since is queued _after_ all previous normal messages and therefore it is processed after them.

In addition, the consumer can shut down without further processing of messages from the producer, if it gets a message with higher priority. Yo achieve this with a standard producer consumer implementation seems impossible to me.

0 Kudos
Message 5 of 13
(5,257 Views)

Thank you for the reply.

I am not sure what to use to make sure the  consumer is "done".

I am thinking I should use the "read self enqueuer" of the consumer, but then I am not sure how to extract any information from it so that I would know when it is empty and therefore when to stop the consumer.

0 Kudos
Message 6 of 13
(5,257 Views)

Send the "Send Normal Stop" message AFTER you have finished sending the "log data" messages. AF is built on queues...it will log all the data then stop.

0 Kudos
Message 7 of 13
(5,257 Views)

No, there is at least until LabVIEW 2014 no easy way to find out how many elements are waiting in an actors priority queue.

The enqueuer would be also the wrong end and the wrong operation. Dequeueing is done automatically, you do not have to care about this.

The idea should be to implement a method "consume.vi" within the consumer actor which gets the output of the producer as parameter.  If the producer actor produces something it sends the message "consume", (which keeps the producers produced data in its private data cluster) to the consumer actor. This message calls the method consume of the consumer actor in its "Do.vi" and puts the parameter from its private data cluster as parameter. The messages are enqueued. If the producers finishes, it sends a stop message to the consumer, which is enqueued at the end of the consumers priority queue. Then every message, which has been enqueued previously is processed before the actor stops.

Conclusion: Everything you need is already directly available from the actor framework. The actor framework itself is a better and more flexible implementation of the producer consumer pattern.

Edit: Typo, conclusion

0 Kudos
Message 8 of 13
(5,257 Views)

I am stuck in the "send data" in a message part. I have attached my implementation and also the results. Is a control reference the wrong way to do this? also there's an extra zero at the end only on the producer side.Producer generated.PNGConsumer consumed.PNGProducer Block diagram.PNGConsumer Block diagram.PNG

0 Kudos
Message 9 of 13
(5,257 Views)

I'm assuming you send the values individually to "Numeric indicator" by calling val(sgnl) property node?

This should work, but is highly inefficient. You could build the array "inside" the actor and create a user event to send the data to the UI. So instead of having reference to the control, you would have a reference to a user event that has an array of doubles as data.


LV 2015SP1
0 Kudos
Message 10 of 13
(5,257 Views)