Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在多个终端窗口中运行多个文件_Python_Subprocess - Fatal编程技术网

如何使用python在多个终端窗口中运行多个文件

如何使用python在多个终端窗口中运行多个文件,python,subprocess,Python,Subprocess,调用正在工作,但它只运行第一个文件。我希望它们都在自己的终端窗口中运行。不要使用,只需依次运行: 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"]) 如果您不想

调用正在工作,但它只运行第一个文件。我希望它们都在自己的终端窗口中运行。

不要使用
,只需依次运行:

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"])
如果您不想让他们在启动下一个进程之前等待进程完成,请使用Popen:

call(["python3", "/home/johngr/psdirc/TestBot1.py"])
call(["python3", "/home/johngr/psdirc/TestBot2.py"])
call(["python3", "/home/johngr/psdirc/TestBot3.py"])
call
将运行args描述的命令。等待命令完成,然后返回returncode属性。其中
Popen
不会等待

如果要确保每个进程以非零退出状态退出,请使用该选项,该选项将针对任何非零退出状态引发CalledProcessError

要为每个打开一个终端,您可以使用
gnome terminal
-e
在终端内执行此选项的参数:

如果要打开选项卡,可以使用
--tab-e

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"])
您似乎没有gnome终端,因此只需将其替换为
lxterminal

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)
不确定是否支持
--tab
选项,我在文档中看不到对它的任何引用。

回答更新后的问题 使用
子流程.Popen

call(['lxterminal', '-e', 'python3 /home/johngr/psdirc/TestBot1.py'])
这将把每一个都放在自己的窗口中。
python3
-i
选项用于在
TestBot3.py
脚本完成后使窗口交互。即使您不希望它是交互式的,拥有它也没有什么坏处,所以您可以在出现问题时进行调试

我做了一个测试,新窗口在退出这个脚本后仍然存在

对原问题的答复 对于生产代码,我不建议这样做,但是:

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)
Python将bash“Good”返回代码0视为False,
操作符是惰性的


当然,这假定您期望每个调用都成功,否则您仍然不会调用所有三个调用。所以你最好用单独的电话线给每个人打电话。如果要清理代码,请将其包装在函数中。

我删除了,并使它们一个接一个,但它仍然打开一个窗口。您正在运行什么?您是说要为每个进程打开一个shell吗?是的,因为每个文件都需要一个shell。每个文件都需要一个shell,或者每个文件都需要一个终端窗口?他们不是一回事,这个问题还不清楚。是否要连续运行它们?一次?“在自己的cmd中”是什么意思?“在多个cmd中”-
cmd.exe
是一个shell;它与终端窗口无关。如果您说“在多个窗口中”,这个问题会更清楚,不过您还需要指定您的操作系统,因为任何类型的windows操作都是特定于操作系统的。我需要它们在多个窗口中运行。@handomebob10,您为什么要更改代码?最近的更改使其中一个答案无效。@handomebob10,不要更改问题中的代码,您需要做的唯一一件事是添加希望每个脚本在其自己的终端窗口中运行的内容,这是问题最重要的部分。哦,nvm,我已经开始工作了:D它是小写的LXTerminal xD。LX终端。谢谢你的帮助。结果是:call([“lxterminal”、“-e”、“python3”、“/home/johngr/psdirc/TestBot2.py”])call([“lxterminal”、“-e”、“python3”、“/home/johngr/psdirc/TestBot3.py”])
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)
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"])