gpt4 book ai didi

python - subprocess.Popen 创建标志

转载 作者:行者123 更新时间:2023-12-05 04:09:58 29 4
gpt4 key购买 nike

我想生成一个子进程,如果一切都在后台发生而不打开新的 Windows 控制台,那就太好了。

首先我认为我的代码有问题,因为它在向子进程发送输入时导致错误。像 String 这样的命令,例如产生错误

Unknown Action: tring

意思是,有时会丢失通过 stdin.write 发送到子进程的输入的第一个字符。

这是代码:

    self.sp = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)

现在我已经尝试了以下方法,一切正常。问题是新开的consoled。

    self.sp = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags = subprocess.CREATE_NEW_CONSOLE
)

有没有另一种方法可以在不打开新的 Windows 控制台的情况下实现这一点?


您的链接将我引向对 CREATE_NEW_WINDOW 标志的描述

A process can also create a console by specifying the CREATE_NEW_CONSOLE flag in a call to CreateProcess. This method creates a new console that is accessible to the child process but not to the parent process. Separate consoles enable both parent and child processes to interact with the user without conflict. If this flag is not specified when a console process is created, both processes are attached to the same console, and there is no guarantee that the correct process will receive the input intended for it. Applications can prevent confusion by creating child processes that do not inherit handles of the input buffer, or by enabling only one child process at a time to inherit an input buffer handle while preventing the parent process from reading console input until the child has finished.

如果可以在不打开窗口的情况下创建一个新控制台就好了。


这似乎解决了问题

bufsize=1

谢谢。

最佳答案

“有没有另一种方法可以在不打开新的 Windows 控制台的情况下实现这一点?”就在这里 !您可以使用创建标志 CREATE_NO_WINDOW,如下所示:

creationflags=subprocess.CREATE_NO_WINDOW

瞧!没有 window !

关于python - subprocess.Popen 创建标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45104218/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com