subprocess exiting in an incomprehensible fashion

W

Will

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
 
M

Maciej Dziardziel

Will said:
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()
 
W

Will

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
 
Joined
Oct 12, 2006
Messages
1
Reaction score
0
I have written a very similar program with same problem.
Have you tried to create your own file descriptor for stdin?

Andy

Maciej Dziardziel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top