如何从基础Python脚本生成新的shell以运行Python脚本?

时间:2022-11-26 07:13:45

I have successfully run several Python scripts, calling them from a base script using the subprocess module:

我已经成功运行了几个Python脚本,使用子进程模块从基本脚本调用它们:

subprocess.popen([sys.executable, 'script.py'], shell=True)

However, each of these scripts executes some simulations (.exe files from a C++ application) that generate some output to the shell. All these outputs are written to the base shell from where I've launched those scripts. I'd like to generate a new shell for each script. I've tried to generate new shells using the shell=True attribute when calling subprocess.call (also tried with popen), but it doesn't work.

但是,这些脚本中的每一个都会执行一些模拟(来自C ++应用程序的.exe文件),这些模拟会向shell生成一些输出。所有这些输出都写入我发布这些脚本的基本shell。我想为每个脚本生成一个新shell。我在调用subprocess.call时尝试使用shell = True属性生成新shell(也尝试使用popen),但它不起作用。

How do I get a new shell for each process generated with the subprocess.call?

如何为subprocess.call生成的每个进程获取一个新shell?

I was reading the documentation about stdin and stdout as suggested by Spencer and found a flag the solved the problem: subprocess.CREATE_NEW_CONSOLE. Maybe redirecting the pipes does the job too, but this seems to be the simplest solution (at least for this specific problem). I've just tested it and worked perfectly:

我正在阅读Spencer建议的关于stdin和stdout的文档,并找到了解决问题的标志:subprocess.CREATE_NEW_CONSOLE。也许重定向管道也可以完成工作,但这似乎是最简单的解决方案(至少对于这个特定的问题)。我刚测试它并完美地工作:

subprocess.popen([sys.executable, 'script.py'], creationflags = subprocess.CREATE_NEW_CONSOLE)

3 个解决方案

#1


10  

Popen already generates a sub process to handle things. You just need to redirect the output pipes. Look at the subprocess documentation, specifically the section on popen stdin, stdout and stderr redirection.

Popen已经生成了一个处理事物的子流程。您只需要重定向输出管道。查看子进程文档,特别是关于popen stdin,stdout和stderr重定向的部分。

If you don't redirect these pipes, it inherits them from the parent. Just be careful about deadlocking your processes.

如果不重定向这些管道,它将从父管道继承它们。只是要小心你的进程死锁。

You wanted additional windows for each subprocess. This is handled as well. Look at the startupinfo section of subprocess. It explains what options to set on windows to spawn a new terminal for each subprocess. Note that it requires the use of the shell=True option.

您希望每个子流程都有其他窗口。这也是处理。查看子进程的startupinfo部分。它解释了在Windows上设置哪些选项来为每个子进程生成一个新终端。请注意,它需要使用shell = True选项。

#2


39  

To open in a different console, do (tested on Windows 7 / Python 3):

要在不同的控制台中打开,请执行(在Windows 7 / Python 3上测试):

from sys import executable
from subprocess import Popen, CREATE_NEW_CONSOLE

Popen([executable, 'script.py'], creationflags=CREATE_NEW_CONSOLE)

input('Enter to exit from this launcher script...')

#3


2  

This doesn't actually answer your question. But I've had my problems with subprocess too, and pexpect turned out to be really helpful.

这实际上并没有回答你的问题。但是我也遇到了子进程的问题,并且pexpect结果非常有用。

#1


10  

Popen already generates a sub process to handle things. You just need to redirect the output pipes. Look at the subprocess documentation, specifically the section on popen stdin, stdout and stderr redirection.

Popen已经生成了一个处理事物的子流程。您只需要重定向输出管道。查看子进程文档,特别是关于popen stdin,stdout和stderr重定向的部分。

If you don't redirect these pipes, it inherits them from the parent. Just be careful about deadlocking your processes.

如果不重定向这些管道,它将从父管道继承它们。只是要小心你的进程死锁。

You wanted additional windows for each subprocess. This is handled as well. Look at the startupinfo section of subprocess. It explains what options to set on windows to spawn a new terminal for each subprocess. Note that it requires the use of the shell=True option.

您希望每个子流程都有其他窗口。这也是处理。查看子进程的startupinfo部分。它解释了在Windows上设置哪些选项来为每个子进程生成一个新终端。请注意,它需要使用shell = True选项。

#2


39  

To open in a different console, do (tested on Windows 7 / Python 3):

要在不同的控制台中打开,请执行(在Windows 7 / Python 3上测试):

from sys import executable
from subprocess import Popen, CREATE_NEW_CONSOLE

Popen([executable, 'script.py'], creationflags=CREATE_NEW_CONSOLE)

input('Enter to exit from this launcher script...')

#3


2  

This doesn't actually answer your question. But I've had my problems with subprocess too, and pexpect turned out to be really helpful.

这实际上并没有回答你的问题。但是我也遇到了子进程的问题,并且pexpect结果非常有用。