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

Don't block on rospy.init_node

asked 2018-02-13 09:37:30 -0500

r00t8 gravatar image


Simple question : is there a way for not blocking on init_node call ?

Doing it (Python) ;

      init_node("MyNode", disable_signals=True)

if roscore is not launched I get, which is normal, this :

      Unable to register with master node [http://localhost:11311/]: master may not be running yet. Will keep trying.

However it's not raising ROSException so it just blocks on it. Can we prevent it ?

Thank you for your help

edit retag flag offensive close merge delete


Thank you guys. It does the job.

r00t8 gravatar image r00t8  ( 2018-02-14 04:12:33 -0500 )edit

Thank you guys. It does the job.

r00t8 gravatar image r00t8  ( 2018-02-14 04:17:37 -0500 )edit

@r00t8: don't post answers unless you are answering your own question.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-14 04:23:58 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-02-13 11:05:21 -0500

gvdhoorn gravatar image

Related question (almost a duplicate): #q10499.

Relevant part of the answer there (from ros_comm/tools/rostopic/src/rostopic):

def _check_master():
    except socket.error:
        raise ROSTopicIOException("Unable to communicate with master!")

_check_master() is not part of the public API of rostopic, but you should be able to use it.

edit flag offensive delete link more



Note that this is not an actual answer (ie: it doesn't make rospy.init_node(..) non-blocking), but your question title and question body don't actually correspond with each other.

This would seem to be an xy problem.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-13 11:06:57 -0500 )edit

answered 2018-02-13 10:45:54 -0500

updated 2018-02-13 11:12:14 -0500

I don't know of anything built-in.

One workaround would be to call rospy.get_published_topics() in a try-except loop that handles socket.error [Errno 111] Connection refused


import time
import errorno
from socket import error as socket_error

import rospy

if __name__ == '__main__':
    ros_master_available = False
    while not rospy.is_shutdown() and not ros_master_available:
             # query ROS Master for published topics   
             ros_master_available = True
        except socket_error as serr:  
            if serr.errno != errno.ECONNREFUSED:               
                raise serr  # re-raise error if its not the one we want     
                print("YOUR STATUS MSG / Waiting for roscore")  
        time.sleep(0.1)  # sleep for 0.1 seconds   

    rospy.init_node("MyNode", disable_signals=True)


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-02-13 09:37:30 -0500

Seen: 1,198 times

Last updated: Feb 14 '18