# what happens to a topic after publisher node is killed

i have a publisher node publishing to a "flag" topic, i have tried doing this using two ways:

continuous publishing:

def flag_publisher(coordinate_x, coordinate_y):

rate = rospy.Rate(5)
pub = rospy.Publisher("/flag", Point, queue_size=5)

msg = Point()
msg.x = float(coordinate_x)
msg.y = float(coordinate_y)
msg.z = 0

while not rospy.is_shutdown():
pub.publish(msg)
rate.sleep()


and publish once only when connected:

def flag_publisher(coordinate_x, coordinate_y):

rate = rospy.Rate(5)
pub = rospy.Publisher("/flag", Point, queue_size=5)
msg = Point()
msg.x = float(coordinate_x)
msg.y = float(coordinate_y)
msg.z = 0

while True:
if pub.get_num_connections() > 0:
pub.publish(msg)
break
else:
rate.sleep()


both ways work fine, but my question is this:

1. since the flag is a static point, which way to do this be preferred and why?
2. in this case, can i use the latch parameter in rospy.publisher to just publish the flag once and exit the node? will my subscribers still be able to get to the last message from the publisher?
edit retag close merge delete

Sort by » oldest newest most voted

If this is actually static, i.e. not changing at all, or at least just very infrequently, it would be best to just publish it in the beginning (make sure, that there is enough time from creating your publisher to actually publish it) and latch it. And then just let the node do nothing.

This way you don't produce any traffic on the topic ("continuous publishing") when it is not needed, and you don't need to check the connections all the time ("publish once when connected"). So this would make your code even more simple.

I'm not sure, but I guess you cannot exit the node when doing this though, as this will also deregister the publisher and thus, even though latched, it will not send the data to new nodes.

more

2

I'm not sure, but I guess you cannot exit the node when doing this

that is correct.

To answer the question in the topic title: when a publisher exits, and it is the only publisher on a topic, no further msgs will be seen on that topic by suscribers. In essence, the topic 'disappears' as well.

( 2018-10-25 01:41:38 -0500 )edit