Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

subprocess - How do you run multiple files in multiple terminal windows using python

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...