Why must rospy.init_node be after rospy.Publisher?
Consider the following simple program
#!/usr/bin/env python
import rospy
import std_msgs
if __name__ == "__main__":
pub = rospy.Publisher('/topic', std_msgs.msg.Int32, queue_size=1)
rospy.init_node('test_py')
pub.publish(0)
When I run rostopic echo /topic
and run the node, it outputs 0
as expected.
However, when I swap the position of init_node
and Publisher
as so
#!/usr/bin/env python
import rospy
import std_msgs
if __name__ == "__main__":
rospy.init_node('test_py')
pub = rospy.Publisher('/topic', std_msgs.msg.Int32, queue_size=1)
pub.publish(0)
It is no longer able to publish.
Why is that the case? Given it would seem more natural (and in line with roscpp) to initialize the node via init_node
before setting up publishers and subscribers.
There is a good chance this is the same as in #q368646.
Publishing a single message milliseconds after having created the
Publisher
most likely doesn't work.