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

How to block subscriber callback?

asked 2017-07-20 01:52:01 -0600

sosoup gravatar image


I am using Python and ROS-Indigo, just curious for some explanation here, if there is any way to block the callback methods of a Subscriber. I am aware that ROS handles the callbacks in separate thread. Please throw some lights. Thanks a lot!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-07-21 01:41:42 -0600

gvdhoorn gravatar image

I think you could either do this by controlling when you call ros::spinOnce(), or write a custom CallbackQueue implementation that you can (en|di)sable whenever needed.

The first would essentially block 'all of ROS' from doing anything, the latter would allow you to selectively block things. See also wiki/roscpp/Overview/Callbacks and Spinning - CallbackQueue::callAvailable() and callOne() and wiki/roscpp/Overview/Callbacks and Spinning - Advanced: Using Different Callback Queues.

edit flag offensive delete link more


Thanks for your response. I am afraid there is no spinOnce() alternative for ROS-Python. And rospy.spin() wont help cause spinning might be from any of the root node or a node that is started earlier than this node which contains the Subscriber and Callback. (Please correct me if I am wrong)

sosoup gravatar image sosoup  ( 2017-07-21 02:39:01 -0600 )edit

You're right that things are different for rospy. I somehow missed that in your question.

It's slightly different, but you could do the same thing in rospy. See swri_rospy fi.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-21 03:07:21 -0600 )edit

I think that's a good link to dig in. I will try to use the resource. Thank you for your support.

sosoup gravatar image sosoup  ( 2017-07-21 04:06:59 -0600 )edit

answered 2017-07-20 14:55:05 -0600

velveteenrobot gravatar image

What do you mean by block? Do you want to sometimes stop the callback form executing when it receives a message? You can have a global or class variable that you toggle when you want change whether to callback will execute or not. And then when the callback is called, it can check that variable and simply return immediately if desired. This should work but there might be a better way to do it.

edit flag offensive delete link more


Placing the global or class variable flag inside the callback method itself wont block the execution of the callback. The use-case here is to hold the execution of the callback method, based on some conditional flag.

sosoup gravatar image sosoup  ( 2017-07-21 01:19:23 -0600 )edit

Question Tools



Asked: 2017-07-20 01:52:01 -0600

Seen: 2,118 times

Last updated: Jul 21 '17