rosrospy

Avoid multiple calls to rospy.init_node()


I am trying to check is a node has already been init another part of a python script? So if I am writting a client class that wraps around a lot of pubs/subs. It needs a node to be active, so it should one should be created. But If I create a node before initializing the object. It will throw the following error:

raise rospy.exceptions.ROSException("rospy.init_node() has already been called with different arguments: "+str(_init_node_args))

So is there a way to check if the scrip has already been initialized? so I can create one if it does not exist and if one does exist and does not try to create another node.


Solution

  • A quick and dirty way to do this is put the init_node call in a try...except block. Something like this:

    try:
        rospy.init_node("NODE_NAME_HERE")
    except rospy.exceptions.ROSException as e:
        print("Node has already been initialized, do nothing")