Ask Your Question

Is a rospy subscriber callback queue processed serially?

asked 2017-10-28 08:20:44 -0500

highWaters gravatar image


From , 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):
        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))
        for i,x in enumerate(
            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
        msg = '\n[&msg ' + str(self.simulation.callback_msg) + ']  callback_fires_msg: \n' + str(np.array(self.simulation.fires[6]))
            "Unexpected error: " + str(sys.exc_info()) + ". Line nr: " + str(sys.exc_info()[2].tb_lineno))
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-03-06 11:50:09 -0500

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-10-28 08:20:44 -0500

Seen: 326 times

Last updated: Mar 06 '19