1

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?

Rui Botelho
  • 752
  • 1
  • 5
  • 18
  • Where do you want the output to go? Do you want it interleaved with your program's output, or sent to a file, or somewhere else? Is that "somewhere else" a thing with a `.write()` method? It's not clear to me what you're expecting to see. – Kevin Nov 05 '14 at 15:47
  • Check this answer: http://stackoverflow.com/a/15899818/4179775 – user3378649 Nov 05 '14 at 15:48
  • @Kevin I want to print to print some stuff on one console and other stuff on another console. – Rui Botelho Nov 05 '14 at 15:54

1 Answers1

1

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.

Rui Botelho
  • 752
  • 1
  • 5
  • 18