468,317 Members | 1,538 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,317 developers. It's quick & easy.

subprocess exiting in an incomprehensible fashion

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
2 1565
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
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.

Similar topics

reply views Thread by Roman Neuhauser | last post: by
4 posts views Thread by Marc Carter | last post: by
2 posts views Thread by Stewart Midwinter | last post: by
3 posts views Thread by Darren Dale | last post: by
5 posts views Thread by Grant Edwards | last post: by
3 posts views Thread by Kevin Walzer | last post: by
9 posts views Thread by Phoe6 | last post: by
10 posts views Thread by JD | last post: by
12 posts views Thread by bhunter | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.