gpt4 book ai didi

python - 为同一进程创建新控制台

转载 作者:太空宇宙 更新时间:2023-11-03 18:06:35 24 4
gpt4 key购买 nike

我试图了解如何创建一个新的控制台来打印内容,也就是说,有多个可用的标准输出。阅读了一些问题后,我只能做到这一点:

from subprocess import Popen, CREATE_NEW_CONSOLE
handle = Popen("cmd", stdin=PIPE, creationflags=CREATE_NEW_CONSOLE)
i.write(b'hello')

但是该消息不会显示在新控制台上。

我有什么选择?

最佳答案

虽然我没有找到如何从新控制台直接创建新的 sdtouts,但我设法使用进程间通信管道获得相同的效果。

new_console.py
from multiprocessing.connection import Client
import sys

address = '\\\\.\pipe\\new_console'
conn = Client(address)
while True:
print(conn.recv())

console_factory.py
from multiprocessing.connection import Listener
from subprocess import Popen, CREATE_NEW_CONSOLE

address = '\\\\.\pipe\\new_console'
listener = Listener(address)

def new_console():
Popen("python new_console.py", creationflags=CREATE_NEW_CONSOLE)
return listener.accept()

c1 = new_console()
c1.send("console 1 ready")
c2 = new_console()
c2.send("console 2 ready")

进一步的改进包括使用循环中的 select 将输入从新控制台发送到主进程。

关于python - 为同一进程创建新控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26761108/

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