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

[rospy] How do you unsubscribe to a topic, from within the callback ?

asked 2012-10-23 06:45:17 -0500

Phuicy gravatar image

In a previous answer a similar issue was addressed but solved in another manner, however the solution suggsted:

def cb_once(msg, subscriber):
    #do processing here

sub_once = None
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once, sub_once)

However since you the right hand side of the last line has to finish before equating to the left hand side, thus sub_once point to wrong place, hence in the cb_once, subscriber.unregister() can't work. Is there another way?

Many Thanks


edit retag flag offensive close merge delete


When you say "can't work" do you mean "I don't think it works" or "I tried it and it doesn't work"? The callback isn't called by Subscriber, although (I think that) there's a small chance that it will fire before Subscriber returns (very small, probably).

Bill Smart gravatar image Bill Smart  ( 2013-07-02 09:55:17 -0500 )edit

You can always wrap the callback code in a try block if you're worried that it will fail.

Bill Smart gravatar image Bill Smart  ( 2013-07-02 09:56:03 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2013-07-02 04:55:26 -0500

Airuno2L gravatar image

I know it's been over a year but I just had this same problem and found this post. Just make sub_once a global variable.

def cb_once(msg):
   #do processing here

global sub_once
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once)
edit flag offensive delete link more


Thanks, I will try but I am skeptical. As the '''rospy.subscriber'' is processed first and the hence the callback ''cb_once" then ''sub_once = ", thus the '''sub_once" in the callback isnt point to the subscriber yet.

Phuicy gravatar image Phuicy  ( 2013-07-02 07:26:23 -0500 )edit

Btw, thank you for replying. It what makes a good community.

Phuicy gravatar image Phuicy  ( 2013-07-02 07:28:07 -0500 )edit

Wouldn't that be fixed by placing a global sub_once in the callback? (And moving the var to the top)

felix k gravatar image felix k  ( 2013-07-03 07:41:38 -0500 )edit

I can confirm that this will work. cb_once will only be called when a message has been received at which point if global sub_once is included in both the callback and in the place making the subscriber the line sub_once.unregister() will reference the subscriber created even below it

inkspell4 gravatar image inkspell4  ( 2017-02-26 20:46:29 -0500 )edit

answered 2014-06-10 19:36:05 -0500

Benjamin Blumer gravatar image

updated 2014-06-10 19:36:29 -0500

An easy way to get a single message is this:

 msg = rospy.wait_for_message("my_topic", MyType)

This returns the message, and shuts down after receiving one message. You can specify a timeout as the third argument. It also doesn't require using a global.

edit flag offensive delete link more



+1. Depending on the purpose though, I think this is cleaner and easier.

130s gravatar image 130s  ( 2014-10-20 11:25:26 -0500 )edit

Question Tools

1 follower


Asked: 2012-10-23 06:45:17 -0500

Seen: 21,976 times

Last updated: Jun 10 '14