Robotics StackExchange | Archived questions

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

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?

Asked by parzival on 2020-06-29 08:03:24 UTC

Comments

Answers

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

Asked by nsprague on 2020-06-29 09:03:01 UTC

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.

Asked by parzival on 2020-06-29 12:22:07 UTC

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.

Asked by Mario Garzon on 2020-06-29 14:17:18 UTC