ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
According to issue https://github.com/ros/ros_comm/issues/1122, 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.
2 | No.2 Revision |
According to issue https://github.com/ros/ros_comm/issues/1122, 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;
}