Reputation: 2296
I want to spawn a subprocess, and it would be good if everything would happen in the background without opening a new Windows console.
First I thought that something is wrong with my code, because it lead to errors while sending input to the subprocess. A Command like String e.g. generated an error
Unknown Action: tring
Meaning, from time to time the first character of the input sent to the subprocess via stdin.write
was missing.
That is the code:
self.sp = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
Now I've tried the following, and everything works fine. The problem is the newly opened consoled.
self.sp = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags = subprocess.CREATE_NEW_CONSOLE
)
Is there another way to achieve this without opening a new Windows console?
Your links lead me to this description of the CREATE_NEW_WINDOW flag
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.
If one could just create a new console without opening a window.
This seems to have solved the problem
bufsize=1
Thank you.
Upvotes: 4
Views: 8874
Reputation: 333
"Is there another way to achieve this without opening a new Windows console?" Yes there is ! You can use the creation flag CREATE_NO_WINDOW, like this:
creationflags=subprocess.CREATE_NO_WINDOW
And voilà ! No windows !
Upvotes: 3