# Producer-consumer problem in ROS

Hello everyone,

I have a newbie problem here. I am trying to implement a ROS Architecture which is similar to a producer-consumer problem. I have one node which is publishing images and a group of nodes that would take the image and do some processing in it. The behaviour that I want to implement is the following: If a consumer node takes one image, I do not want the others to receive that same image. Using the default image_transport publisher, I suppose that all the subscribers will receive the same image at a time. How can I avoid this?

edit retag close merge delete

Sort by » oldest newest most voted

I think using a service for this might be an easy solution. The producer node would not publish, but queue images and provide a service consume_image that returns the next image or fails when empty.

Consumer nodes call this service whenever they are ready to process the next image.

more

You are right that all topic listeners will receive all images. You will probably need to coordinate the consumers in a centralized way. So you'll need a centralized instance which makes sure each consumer knows which images he has to ignore. There are several ways in which such a centralized coordination can be achieved, depending on your exact setup and needs. E.g. is the number of consumers known beforehand and fixed, or does it change? Do you assume all consumers have the same processing time per image, or is that variable as well? Do you need the consumers to produce an ordered output of produced images, or are their work producets independent of each other?

Note that you are free to create new topics for that purpose, and you can republish images if that's useful for you. So a central node could listen to the main topic and distribute images over individual topics. Or consumer nodes could be chained with topics, where each consumer either consumes the image, or publishes it to the next. Not saying either is a good solution for you, just making sure you see these possibilities as well.

more