subprocess exiting in an incomprehensible fashion

Will wholcomb at gmail.com
Sat Oct 22 13:17:03 EDT 2005


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




More information about the Python-list mailing list