从 python 脚本的多个终端窗口运行多个命令 [英] Running multiple commands from multiple terminal windows from python script
问题描述
我有一个问题,我需要编写一个python脚本,它基本上打开一个终端窗口并在其中启动一个node js服务器,然后打开另一个终端窗口并在其中启动一个java程序.
I have a problem where I need to write a python script, which basically opens one terminal windows and starts a node js server in it, then opens another terminal windows and starts a java program in it.
如果我运行两个 subprocess.call() 函数,它们会在同一个终端窗口中运行.
If I run two subprocess.call() functions they operate in the same terminal window.
有没有办法做到这一点?
Is there a way I can do this?
谢谢.:)
推荐答案
使用 subprocess.Popen:
Use subprocess.Popen:
这将为每个机器人创建新窗口并在其中运行程序.python3 的 -i 选项是在 TestBot3.py 脚本完成后使窗口具有交互性.
This will create new window for each bot and run program in it. The -i option for python3 is to make the window interactive after the TestBot3.py script finishes.
from subprocess import Popen, PIPE
bot1 = Popen(["lxterminal", "-e", "python3", "-i", "TestBot1.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot2 = Popen(["lxterminal", "-e", "python3", "-i", "TestBot2.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot3 = Popen(["lxterminal", "-e", "python3", "-i", "TestBot3.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
或者你可以使用from subprocess import call
call(["python3", "TestBot1.py"])
call(["python3", "TestBot2.py"])
call(["python3", "TestBot3.py"])
要为每个人打开一个终端,您可以使用 gnome-terminal 和 -e 在终端内执行此选项的参数:
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 TestBot1.py"])
call(['gnome-terminal', '-e', "python3 TestBot2.py"])
call(['gnome-terminal', '-e', "python3 TestBot3.py"])
这篇关于从 python 脚本的多个终端窗口运行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!