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?

3
  • 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. Commented Nov 5, 2014 at 15:47
  • Check this answer: stackoverflow.com/a/15899818/4179775 Commented Nov 5, 2014 at 15:48
  • @Kevin I want to print to print some stuff on one console and other stuff on another console. Commented Nov 5, 2014 at 15:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.