How do you run multiple files in multiple terminal

2019-02-19 22:31发布

from subprocess import call
call(["python3", "/home/johngr/psdirc/TestBot1.py"]) and call(["python3", "/home/johngr/psdirc/TestBot2.py"]) and call(["python3", "/home/johngr/psdirc/TestBot3.py"])

The call is working but it only runs the first file. I want them all to run in their own terminal windows.

2条回答
Emotional °昔
2楼-- · 2019-02-19 22:36

Don't use and just run one after the other:

call(["python3", "/home/johngr/psdirc/TestBot1.py"])
call(["python3", "/home/johngr/psdirc/TestBot2.py"])
call(["python3", "/home/johngr/psdirc/TestBot3.py"])

If you don't want them to wait for the process to finish before starting the next use Popen:

 Popen(["python3", "/home/johngr/psdirc/TestBot1.py"])
 Popen(["python3", "/home/johngr/psdirc/TestBot2.py"])
 Popen(["python3", "/home/johngr/psdirc/TestBot3.py"])

call will Run the command described by args. Wait for command to complete, then return the returncode attribute. where Popen won't wait.

If you want to be sure each process exits with a non-zero exit status use check_call which will raise a CalledProcessError for any non-zero exit status.

To open a terminal for each you can use gnome-terminal with -e Execute the argument to this option inside the terminal:

call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot1.py"])
call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot2.py"])
call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot3.py"])

If you want to open tabs you can use --tab -e:

cmd =['gnome-terminal', '--tab', '-e', 'python3 /home/johngr/psdirc/TestBot1.py',
      '--tab', '-e','python3 /home/johngr/psdirc/TestBot2.py','--tab', '-e', 
      'python 3 /home/johngr/psdirc/TestBot3.py']
call(cmd)

You don't seem to have gnome-terminal so just replace it with lxterminal:

call(['lxterminal', '-e', 'python3 /home/johngr/psdirc/TestBot1.py'])

Not sure if --tab option is supported or not, I don't see any reference to it in the documentation.

查看更多
ら.Afraid
3楼-- · 2019-02-19 22:47

Answer to updated question

Use subprocess.Popen:

from subprocess import Popen, PIPE

bot1 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot1.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot2 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot2.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot3 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot3.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)

This will put each into its own window. The -i option for python3 is to make the window interactive after the TestBot3.py script finishes. It doesn't hurt to have, even if you don't expect it to be interactive, so you can debug in case something does go wrong.

I did a test, and the new windows persist after exiting this script.

Answer to original question

I don't recommend this for production code, but:

not call(["python3", "/home/johngr/psdirc/TestBot1.py"]) \
    and not call(["python3", "/home/johngr/psdirc/TestBot2.py"]) \
    and not call(["python3", "/home/johngr/psdirc/TestBot3.py"])

Python treats the bash "Good" return code of 0 as False, and the and operator is lazy.

This of course presumes you expect each call to succeed, otherwise you will still not call all three. So you're much better off calling each on a separate line. Wrap it in a function if you want to clean up the code.

查看更多
登录 后发表回答