ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

getNumSubscribers and TopicManager

asked 2020-03-05 07:24:33 -0600

billkotsias gravatar image

I am trying to limit the number of subscribers a topic can get to just 1. However, I see that publisher.getNumSubscribers() and subscriber.getNumPublishers() only return either 0 or 1!

I also tried to get the TopicManager like auto manager = ros::TopicManager::instance(); // check for multi-instantiation of singleton modules and using it to check the number of subscribers per topic like: manager->getNumSubscribers(from_topic) but this ALWAYS returns 0!

The code is pretty simple, I create subscribers and publishers to the same topics, in the same function call. Between each creation, I also call ros::spinOnce().

Eventually, getNumSubscribers() increases from 0 to 1 but never to more.

Is this a feature or a bug?

edit retag flag offensive close merge delete


I am trying to limit the number of subscribers a topic can get to just 1

can you tell us how you plan to do that?

gvdhoorn gravatar image gvdhoorn  ( 2020-03-05 07:33:05 -0600 )edit

I would just throw if I saw that 2 publishers or 2 subscribers are on the same topic. So programmer will know he mustn't do it. But seems this isn't possible in current Ros version. They don't go over 1 (maybe cause they are in same process)

billkotsias gravatar image billkotsias  ( 2020-03-05 15:04:09 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-02-08 18:26:34 -0600

peci1 gravatar image

updated 2022-05-17 14:23:13 -0600

According to issue, this is most probably just a badly named function. Ros internally coalesces all publishers or subscribers to a topic inside the same node (process) into a single "transport connection". What getNumSubscribers() and getNumPublishers() return is the number of these transports, not the number of individual Subscriber or Publisher instances. So it mostly corresponds to the number of subscribing nodes.

The following code can give you a better idea about the number of acutal registered callbacks, although it uses a hack to access a private field of TopicManager:

// HACK: we need to access TopicManager::subscriptions_
#define private public
#include <ros/topic_manager.h>
#undef private
#include <ros/subscription.h>

size_t getNumSubscriptions(const std::string& topic)
    size_t num = 0;
    auto subs = ros::TopicManager::instance()->subscriptions_;
    for (const auto& sub : subs)
        if (sub->getName() == topic)
            num += sub->getNumCallbacks();
    return num;
edit flag offensive delete link more


Also (this just happened to me), if you first create a subscriber and then you advertise a publisher to the same topic but with wrong type, the publisher is not counted.

peci1 gravatar image peci1  ( 2022-02-08 18:28:02 -0600 )edit

Question Tools



Asked: 2020-03-05 07:24:33 -0600

Seen: 743 times

Last updated: May 17 '22