Rui Botelho
Rui Botelho

Reputation: 751

Create new console for the same process

I am trying to understand how to create a new console to print stuff, that is, have more than one stdout available. After reading some questions, I only managed to do this:

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

But the message doesnt show up on the new console.

What are my options?

Upvotes: 1

Views: 881

Answers (1)

Rui Botelho
Rui Botelho

Reputation: 751

Altough I didnt find how to directly create new sdtouts from new consoles, I managed to get the same effect using inter-process communication pipes.

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")

Further improvements include sending input from new consoles to the main process, using select in the loop.

Upvotes: 1

Related Questions