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

Is it possible to launch multiple nodes from one python script?

asked 2014-06-24 18:27:20 -0600

nckswt gravatar image

I see that rospy.init_node() can only be called once per process in the official documentation on nodes.

I'm trying to write a script that allows for two-way communication (one message publisher and one message subscriber) to interact with a single object. If I have to keep the publisher in one script and the subscriber in another, I'll have a bit of a mess on my hands if I want to interact with the single object.

Is there a simple way of spawning multiple nodes (one talker, one listener, in this case) from one single process?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-06-24 18:55:59 -0600

updated 2017-10-04 19:58:35 -0600

I don't think so.

Roslaunch is what is used to launch multiple nodes. There was a package called roslaunch_caller to launch nodes programatically but it is not supported and its recommended not to use.

perhaps a reevaluation of your requirements is in order as it would be much simpler to pass information within a process using traditional programming concepts, rather than using publishers and subscribers via TCP.

Update

You can run launch files from python using a cmd line package such as subprocess. To change your launch environment you can add arguments to your launch file which can be used as parameters that can be accessed by nodes, and specified by the script.

e.g.

ret = sp.call('roslaunch pkg_name launchfile_name.launch arg1:="{}" arg2:={} arg3:="{} {} {} {}" arg4:="{}"'.format(param1, param2, param3, param4, param5, param6, param7), shell=True)

using shell=True is required to import the ROS_WORKSPACE setup, but as noted in the subprocess documentation it can be a security hazard.

edit flag offensive delete link more

Comments

Hmm, that'll demand a near-complete rework of my system. I assume that launching nodes in individual threads wouldn't work either. I'll keep looking into it and see if I can find an answer. Thanks.

nckswt gravatar image nckswt  ( 2014-06-24 19:18:41 -0600 )edit

Is there still no solution to this problem in 2017?

JaimeOrellana gravatar image JaimeOrellana  ( 2017-07-11 13:41:49 -0600 )edit
0

answered 2017-08-16 02:45:34 -0600

chrisLB gravatar image

It is possible! I am using something like the following code in my rostests to setup the node environment if necessary.

import roslaunch
package = 'PACKACKE_NAME'
executable = 'YOUR_SCRIPT.py'
node_name = 'YOUR_NODE_NAME'
node = roslaunch.core.Node(package=package, node_type=executable, name=node_name)
launch = roslaunch.scriptapi.ROSLaunch()
launch.start() 
process = launch.launch(node)
edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2014-06-24 18:27:20 -0600

Seen: 7,646 times

Last updated: Oct 04 '17