By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

subprocess exiting in an incomprehensible fashion

P: n/a
I have this bit of code:

#!/usr/bin/python

import subprocess

calc = subprocess.Popen("dc", stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
max = 5
for value in range(1, max):
calcout, calcerr = calc.communicate("%d\n" % value)
print("Post Communicate Value %d: %s (%s) [%d]" %
(value, calcout, calcerr, calc.returncode))
if value > 1:
calcout, calcerr = calc.communicate("*\n")
calcout, calerr = calc.communicate("p\n")
print("Value: %d = %d" %
(calcout, reduce(lambda x,y: x * y, range(1, max))))
calc.communicate("q\n")
status = calc.wait()
print "Exited with: %d" % status

### End Code

After the first number is input, the subprocess is exiting after the
first communicate. I do not understand why. I have tried to accomplish
the same thing with os.popen and popen2.Popen. I can't get anything to
work and I am having a difficult time finding examples that include
both redirected input and output from a subprocess. I am wanting to do
work with a more complicated program, but was looking to get something
simple working first.

Any help would be much appreciated. I have been hunting quite a bit for
he answer to no avail.

Will Holcomb

Oct 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Will wrote:
After the first number is input, the subprocess is exiting after the
first communicate. I do not understand why.
As help(calc.communicate) says:

Read data from stdout and stderr, until end-of-file is reached.
Wait for process to _terminate_.
Any help would be much appreciated. I have been hunting quite a bit for
he answer to no avail.


Simplest solution:

#!/usr/bin/python2.4
import subprocess

calc = subprocess.Popen("dc", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
max = 5
for value in range(1, max):
calc.stdin.write("%d\n" % value)
if value > 1:
calc.stdin.write("*\n")
calc.stdin.write("p\n")
calc.stdin.write("q\n")
calc.stdin.flush()
status = calc.wait()
print calc.stdout.read()

--
Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)
www.fiedzia.prv.pl
Oct 21 '05 #2

P: n/a
Well, I'll be damned... Thank you very much. I'm still getting a little
tripped up with blocking IO and whatnot, but I think I can work that
out. This was a real help, thanks again.

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import subprocess
import random
import re
import os
import time
import select

calc = subprocess.Popen("dc", stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
max = random.Random().randint(10, 100)
for value in range(1, max):
calc.stdin.write("%d\n" % value)
if value > 1:
calc.stdin.write("*\n")
calc.stdin.write("p\n")

select.select([calc.stdout.fileno()], [], [])
time.sleep(.1) # Still not always getting the result

string = os.read(calc.stdout.fileno(), 500)

print "String: ", string
dcproduct, repcount = re.subn("\\\|\\s", "", string)
dcproduct = int(dcproduct)
pyproduct = reduce(lambda x,y: x * y, range(1, max))
if dcproduct == pyproduct:
print "*(1,%d):" % (max - 1)
else:
print "Products don't match: n = %d" % (max - 1)
print " %d" % dcproduct
print " %d" % pyproduct

calc.stdin.write("q\n")
status = calc.wait()
print "Exited with: %d" % status

Oct 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.