Ask Your Question
0

rospy init_node() inside imported file

asked 2017-07-17 18:57:02 -0600

zplizzi gravatar image

I'd like to perform a rospy.init_node() inside a file imported by another file. However, this doesn't seem to work - the program hangs whenever I try this.

Example (broken, both files in same folder):

main.py:

import test

test.py:

   import rospy
   rospy.init_node("node_name")

Running python main.py results in the program hanging. However, running python test.py works.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2017-07-18 08:50:53 -0600

updated 2017-07-18 08:51:12 -0600

I have a similar project, it works like that:

  1. Inside the project folder, I have a folder called packages, with a __init__.py inside. It's empty, just to make the module available
  2. In the packages folder yet, a file called initialize.py, where I have:

    import rospy as _rospy from std_msgs.msg import String as _String, Empty as _Empty

    def init():

        _rospy.init_node('controller', anonymous=True)
        global _sub
        _sub = _rospy.Subscriber("/model_name", _String, _nameCallback)
        global _pub
        _pub = _rospy.Publisher(_controllerName+'/encoder_updated',_Empty)
    
  3. In the root folder, my script that imports the initialize library:

    import packages.initialize

    initialize.init()

edit flag offensive delete link more

Comments

Yep, this works - since the init_node() call is in a method called by the main file, it seems to work. What I can't figure out is why it doesn't work in the example I described - where init_node() is called implicitly during the import. This method works, but still curious why the other doesn't.

zplizzi gravatar imagezplizzi ( 2017-07-18 11:23:06 -0600 )edit

I think the answers for this question https://stackoverflow.com/questions/6... can explain better. The question is the opposite of yours, but there are some explanation there.

marcoarruda gravatar imagemarcoarruda ( 2017-07-18 11:33:05 -0600 )edit

Yep, I understand how that works. Python will run all the code in an imported file upon import. What my question is asking is why the init_node() function doesn't work when run in that way (versus being run directly from the main script).

zplizzi gravatar imagezplizzi ( 2017-07-18 13:01:19 -0600 )edit
0

answered 2019-11-18 14:17:28 -0600

MartinK gravatar image

Not sure where this comes from. My workaround is by importing the module from imp. No need to modify the module itself.

main.py

import imp
test = imp.load_source("test", "test.py")
edit flag offensive delete link more
0

answered 2017-08-02 10:17:08 -0600

naveedhd gravatar image

updated 2017-08-02 10:17:30 -0600

Althought this is mentioned here that if init_node is called with disable_signals=True we can call init_node from separate thread, but it still gets stuck here. (os.getpid() is different for main and test)

Now you can go down in impl/init.py and impl/masterslave.py to look deeper of how the different threads affect the node registration with the master.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-07-17 18:57:02 -0600

Seen: 530 times

Last updated: Nov 18