ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
When you subscribe to a topic with rospy
the callback is given its own thread automatically to process the data from the topic. What seems to be happening here is that all threads are given a single method to do their computation (individual_callback()
), forcing each thread to compete for time. This is why you see the roughly linear relationship between calculation speed and number of subscribed agents.
So the parallelism is already happening, but its being counter acted by having only a single processing callback. The solution is to use a unique callback for each subscriber.
2 | No.2 Revision |
When you subscribe to a topic with rospy
the callback is given its own thread automatically to process the data from the topic. What seems to be happening here is that all threads are given a single method to do their computation (individual_callback()
), forcing each thread to compete for time. This is why you see the roughly linear relationship between calculation speed and number of subscribed agents.
So the parallelism is already happening, but its being counter acted by having only a single processing callback. The solution is to use a unique callback for each subscriber. E.g.
class contains:
def __init__(self):
self.agent_msgs = [ ] #for speed, this should by a numpy array
for i in range(0, self.agents):
name = 'agent%s' % (i + 1)
self.center_dict[name] = 0.0
def agent_cb0(msg):
self.agent_msgs[0] = msg
def agent_cb1(msg):
self.agent_msgs[1] = msg
....
def setup():
'''
Subscribers and publisher
'''
self.agent_dictionary = {}
self.contains_dict = {}
rospy.Subscriber('/agent0', IndividualPositions, self.agent_cb0, queue_size=1)
rospy.Subscriber('/agent1', IndividualPositions, self.agent_cb1, queue_size=1)
...