If I spawn a new subprocess
in python with a given command (let's say I start the python interpreter with the python
command), how can I send new data to the process (via STDIN)?
Asked
Active
Viewed 3.2k times
14

S.Lott
- 384,516
- 81
- 508
- 779
-
1if it's a shell you want to automate interaction with, `pexpect` is great for this kind of thing. – wim Feb 17 '12 at 04:38
-
A tunnel created by Subprocess to fire multiple commands can't be kept alive. for achieving this you can look into paramiko, for other stuff like subprocess stdin, stdout, stderr you can go through this link [python subprocess](http://docs.python.org/library/subprocess.html), since this is your first python project it is better you read and try out stuff. – avasal Feb 17 '12 at 04:33
2 Answers
18
Use the standard subprocess module. You use subprocess.Popen() to start the process, and it will run in the background (i.e. at the same time as your Python program). When you call Popen(), you probably want to set the stdin, stdout and stderr parameters to subprocess.PIPE. Then you can use the stdin, stdout and stderr fields on the returned object to write and read data.
Untested example code:
from subprocess import Popen, PIPE
# Run "cat", which is a simple Linux program that prints it's input.
process = Popen(['/bin/cat'], stdin=PIPE, stdout=PIPE)
process.stdin.write(b'Hello\n')
process.stdin.flush()
print(repr(process.stdout.readline())) # Should print 'Hello\n'
process.stdin.write(b'World\n')
process.stdin.flush()
print(repr(process.stdout.readline())) # Should print 'World\n'
# "cat" will exit when you close stdin. (Not all programs do this!)
process.stdin.close()
print('Waiting for cat to exit')
process.wait()
print('cat finished with return code %d' % process.returncode)
-
So let's say I need several processes running at once. Is it a better idea to spawn lots of subprocesses with `Popen()`, or to 'fork'? – Feb 17 '12 at 13:02
-
2Popen does fork the processes. It's only a question of whether you want to do the low level fork yourself or whether you want it wrapped up in a higher level library. The end result is exactly the same. – Michael Dillon Feb 17 '12 at 16:42
-
So several concurrent Popens won't bog down the system? Thanks, I'll give this a try. – Feb 17 '12 at 16:57
2
Don't.
If you want to send commands to a subprocess, create a pty and then fork the subprocess with one end of the pty attached to its STDIN.
Here is a snippet from some of my code:
RNULL = open('/dev/null', 'r')
WNULL = open('/dev/null', 'w')
master, slave = pty.openpty()
print parsedCmd
self.subp = Popen(parsedCmd, shell=False, stdin=RNULL,
stdout=WNULL, stderr=slave)
In this code, the pty is attached to stderr because it receives error messages rather than sending commands, but the principle is the same.

cs95
- 379,657
- 97
- 704
- 746

Michael Dillon
- 31,973
- 6
- 70
- 106
-
4Don't use pty.openpty() with subprocess - that's over complex. Use subprocess.PIPE, and let the subprocess module take care of it for you. (As well as being simpler and more standard, that will use a pipe, which is lighter-weight than a PTY). – user9876 Feb 17 '12 at 11:57