0

I want to write a python script which should create a process in the background, redirect its stdin, stdout and stderr for communication with that process (this executable is my program) to a separate virtual streams.

I need to run several instances of my program at once from python script and i need a proper solution to receive/send messages to their overriden streams. I have no experience on sub/process/streams in python, i am looking for a diamong code sample, thank you..

Croll
  • 3,213
  • 6
  • 24
  • 54

1 Answers1

1

What you're after is subprocess.Popen:

import subprocess
p = subprocess.Popen(["mycmd", "--somearg"], stdout=subprocess.PIPE)
out, err = p.communicate()
Mark R.
  • 1,083
  • 6
  • 13
  • Does p.communicate() returns array? And how do i redirect stdin? – Croll Feb 17 '15 at 15:59
  • If you pass stdin=PIPE, the communicate call will write to the process' stdin descriptor. – Mark R. Feb 17 '15 at 16:03
  • Found this: https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate and https://docs.python.org/2/library/subprocess.html#subprocess.Popen Also useful: http://stackoverflow.com/questions/9886654/difference-between-communicate-and-stdin-write-stdout-read-or-stderr-read and http://stackoverflow.com/questions/2715847/python-read-streaming-input-from-subprocess-communicate – Croll Feb 17 '15 at 18:02