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

How to execute the rest of the python script while launching and running the launch files in the background?

asked 2020-06-29 08:03:24 -0500

parzival gravatar image

I want to launch multiple launch files inside a python script. I'm planning to use the os.system("roslaunch ...") command from os library.

Since I have multiple launch files, it will look something like this:

os.system("roslaunch 1 & roslaunch 2 & roslaunch 3") command,

but these commands show output of the nodes in roslaunch on the terminal and the python code isn't executed further until these nodes are terminated, which I don't want. I want to execute the command and launch the nodes, keep these roslaunch files running and want to get back to the rest of the code loop while the processes still run.

How can I do that?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-06-29 09:03:01 -0500

nsprague gravatar image

You should check out the subprocess module in the Python standard libraries.

edit flag offensive delete link more

Comments

Hi, I actually tried subprocess too, but it doesn't work. For example, if my code is:

import subprocess
subprocess.Popen(["roslaunch", "pkg", "file.launch",])
print("then this was printed")

Then my terminal will have output from the roslaunch, and program doesn't proceed further to execute the print statement till I terminate the roslaunch command.

What I require is the python code executes the roslaunch command and goes on to execute the print statement, regardless of whether the roslaunch command is still running or terminated.

parzival gravatar image parzival  ( 2020-06-29 12:22:07 -0500 )edit
1

HI, I think subprocess should work. Here's a piece of code I've used to do exactly that

command = "roslaunch {0} {1}".format(pkg, launchfile)
my_env = os.environ.copy()
p = subprocess.Popen(command, shell=True, env=my_env)

Hope that works for you.

Mario Garzon gravatar image Mario Garzon  ( 2020-06-29 14:17:18 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-29 08:03:24 -0500

Seen: 981 times

Last updated: Jun 29 '20