1

I'm having a very strange issue with Python's subprocess.Popen. I'm using it to call several times an external exe and keep the output in a list.

Every time you call this external exe, it will return a different string. However, if I call it several times using Popen, it will always return the SAME string. =:-O

It looks like Popen is returning always the same value from stdout, without recalling the exe. Maybe doing some sort of caching without actually calling again the exe.

This is my code:

def get_key():

    from subprocess import Popen, PIPE

    args = [C_KEY_MAKER, '/26', USER_NAME, ENCRYPTION_TEMPLATE, '0', ]
    process = Popen(args, stdout=PIPE)
    output = process.communicate()[0].strip()
    return output

if __name__ == '__main__':
    print get_key() # Returns a certain string
    print get_key() # Should return another string, but returns the same!

What on Earth am I doing wrong?!

3
  • When you run the C_KEY_MAKER command twice -- directly on the command line -- what does it produce? Commented Apr 4, 2009 at 13:57
  • Two different strings, as expected. Commented Apr 4, 2009 at 15:03
  • @Fernando: Actually not expected by me. I have my doubts that the command-line app has appropriate random number seeds. There must be some environment variable difference between the subprocess and the command-line version. Commented Apr 4, 2009 at 16:26

4 Answers 4

3

It is possible (if C_KEY_MAKER's random behaviour is based on the current time in seconds, or similar) that when you run it twice on the command line, the time has changed in between runs and so you get a different output, but when python runs it, it runs it twice in such quick succession that the time hasn't changed and so it returns the same value twice in a row.

Sign up to request clarification or add additional context in comments.

Comments

1

Nothing. That works fine, on my own tests (aside from your indentation error at the bottom). The problem is either in your exe. or elsewhere.

To clarify, I created a python program tfile.py

cat > tfile.py
#!/usr/bin/env python
import random
print random.random()

And then altered tthe program to get rid of the indentation problem at the bottom, and to call tfile.py . It did give two different results.

2 Comments

I'm running on Windows, could that be the problem?
It's possible, but I doubt it. You could run such an example yourself and see if anything weird crops up, but that would be a serious bug in the subprocess module-- it should work identically in Linux and Windows, and it should work the way it does on my machine.
1

I don't know what is going wrong with your example, I cannot replicate this behaviour, however try a more by-the-book approach:

def get_key():

    from subprocess import Popen, PIPE

    args = [C_KEY_MAKER, '/26', USER_NAME, ENCRYPTION_TEMPLATE, '0', ]
    output = Popen(args, stdout=PIPE).stdout
    data = output.read().strip()
    output.close()
    return data

Comments

0

Your code is not executable as is so it's hard to help you out much. Consider fixing indentation and syntax and making it self-contained, so that we can give it a try.

On Linux, it seems to work fine according to Devin Jeanpierre.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.