Robotics StackExchange | Archived questions

Is a rospy subscriber callback queue processed serially?

Hello,

From https://answers.ros.org/question/9543/rospy-threading-model/, I gathered that in fact such processing should happen serially. However. I have the following piece of code that behaves in a weird way. I have multiple nodes that are subscribed and publish to the same topic. Normally, on each call of the callback function, three lines should be printed, and it should look as:

[#msg x]

%

[&msg x], where x is incremented on each call (and is modified only in this callback function), and is declared somewhere else.

If I comment the lock as in the snippet, I'll get outputs like:

[#msg 1]

%

[&msg 2] ---> this should be 1!


[#msg 2]

%

[&msg 2]

So it seems that a new thread is spawned every-time. Has the rospy thread model changed?

rospy.Subscriber('/environment/fires_pb', Fires_Info, self.callback_fires_msg)
self.publish_fires = rospy.Publisher('/environment/fires_pb', Fires_Info, queue_size=200)

...

def callback_fires_msg(self, data):
    try:
        #self.myknowledge.lock_cb.acquire()
        self.simulation.callback_msg += 1
        msg = '\n[#msg ' + str(self.simulation.callback_msg) + ']  callback_fires_msg: \n' + str(np.array(self.simulation.fires))
        msg += '\n%callback_fires_msg, data: ' + str(np.array(data))
        rospy.loginfo(msg)
        for i,x in enumerate(data.id):
            for j, a in enumerate(self.simulation.fires[0]):
                if a == x:
                    self.simulation.fires[3][j] = data.intensity[i]
                    self.simulation.fires[4][j] = data.victims[i]
                    self.simulation.fires[5][j] = data.status[i]
                    self.simulation.fires[6][j] = 1
                    break
        msg = '\n[&msg ' + str(self.simulation.callback_msg) + ']  callback_fires_msg: \n' + str(np.array(self.simulation.fires[6]))
        rospy.loginfo(msg)
        #self.myknowledge.lock_cb.release()
    except:
        rospy.loginfo(
            "Unexpected error: " + str(sys.exc_info()) + ". Line nr: " + str(sys.exc_info()[2].tb_lineno))

Asked by highWaters on 2017-10-28 08:20:44 UTC

Comments

Answers

As others have pointed out in the thread you refer to: Yes, the threading model is different. As far as I understand each callback gets its own thread per connection.

Asked by d.chronopoulos on 2019-03-06 12:47:18 UTC

Comments