How do you run multiple files in multiple terminal windows using python(如何使用 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.
推荐答案
不要使用而
一个接一个地运行:
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"])
如果您不希望他们在开始下一次使用 Popen 之前等待进程完成:
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
将 运行 args 描述的命令.等待命令完成,然后返回 returncode 属性. Popen
不会等待.
如果您想确保每个进程以非零退出状态退出,请使用 check_call 对于任何非零退出状态都会引发 CalledProcessError.
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.
要为每个终端打开一个终端,您可以使用 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 /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"])
如果你想打开标签,你可以使用 --tab -e
:
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)
您似乎没有 gnome-terminal 所以只需将其替换为 lxterminal
:
You don't seem to have gnome-terminal so just replace it with lxterminal
:
call(['lxterminal', '-e', 'python3 /home/johngr/psdirc/TestBot1.py'])
不确定是否支持 --tab
选项,我在文档中没有看到对它的任何引用.
Not sure if --tab
option is supported or not, I don't see any reference to it in the documentation.
这篇关于如何使用 python 在多个终端窗口中运行多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 python 在多个终端窗口中运行多个文件


基础教程推荐
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01