具有隐藏窗口的跨平台子流程

时间:2022-02-10 12:21:04

I want to open a process in the background and interact with it, but this process should be invisible in both Linux and Windows. In Windows you have to do some stuff with STARTUPINFO, while this isn't valid in Linux:

我想在后台打开一个进程并与之交互,但这个进程在Linux和Windows中都应该是不可见的。在Windows中,您必须使用STARTUPINFO执行某些操作,而这在Linux中无效:

ValueError: startupinfo is only supported on Windows platforms

ValueError:仅在Windows平台上支持startupinfo

Is there a simpler way than creating a separate Popen command for each OS?

有没有比为每个操作系统创建单独的Popen命令更简单的方法?

if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    proc = subprocess.Popen(command, startupinfo=startupinfo)
if os.name == 'posix':
    proc = subprocess.Popen(command)    

4 个解决方案

#1


32  

You can reduce one line :)

你可以减少一行:)

startupinfo = None
if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)

#2


12  

Just a note: for Python 2.7 I have to use subprocess._subprocess.STARTF_USESHOWWINDOW instead of subprocess.STARTF_USESHOWWINDOW.

请注意:对于Python 2.7,我必须使用subprocess._subprocess.STARTF_USESHOWWINDOW而不是subprocess.STARTF_USESHOWWINDOW。

#3


3  

I'm not sure you can get much simpler than what you've done. You're talking about optimising out maybe 5 lines of code. For the money I would just get on with my project and accept this as a consquence of cross-platform development. If you do it a lot then create a specialised class or function to encapsulate the logic and import it.

我不确定你能比你做的更简单。你在谈论优化5行代码。为了钱我会继续我的项目,并接受这作为跨平台开发的一个序列。如果你做了很多,那么创建一个专门的类或函数来封装逻辑并导入它。

#4


1  

You can turn your code into:

您可以将代码转换为:

params = dict()

if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    params['startupinfo'] = startupinfo

proc = subprocess.Popen(command, **params)

but that's not much better.

但那并没有好多少。

#1


32  

You can reduce one line :)

你可以减少一行:)

startupinfo = None
if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)

#2


12  

Just a note: for Python 2.7 I have to use subprocess._subprocess.STARTF_USESHOWWINDOW instead of subprocess.STARTF_USESHOWWINDOW.

请注意:对于Python 2.7,我必须使用subprocess._subprocess.STARTF_USESHOWWINDOW而不是subprocess.STARTF_USESHOWWINDOW。

#3


3  

I'm not sure you can get much simpler than what you've done. You're talking about optimising out maybe 5 lines of code. For the money I would just get on with my project and accept this as a consquence of cross-platform development. If you do it a lot then create a specialised class or function to encapsulate the logic and import it.

我不确定你能比你做的更简单。你在谈论优化5行代码。为了钱我会继续我的项目,并接受这作为跨平台开发的一个序列。如果你做了很多,那么创建一个专门的类或函数来封装逻辑并导入它。

#4


1  

You can turn your code into:

您可以将代码转换为:

params = dict()

if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    params['startupinfo'] = startupinfo

proc = subprocess.Popen(command, **params)

but that's not much better.

但那并没有好多少。