subprocess.Popen and ordering writes to stdout and stderr

Discussion in 'Python' started by Chris Withers, Dec 17, 2009.

  1. Hi All,

    I have this simple function:

    def execute(command):
    process = Popen(command.split(),stderr=STDOUT,stdout=PIPE)
    return process.communicate()[0]

    ...but my unit test for it fails:

    from testfixtures import tempdir,compare
    from unittest import TestCase

    class TestExecute(TestCase):

    def test_out_and_err(self,d):
    path = d.write('','\n'.join((
    "import sys",
    execute(sys.executable+' '+path))


    @@ -1,4 +1,4 @@

    ....the order of the writes isn't preserved.
    How can I get this to be the case?

    Chris Withers, Dec 17, 2009
    1. Advertisements

  2. Chris Withers

    Lie Ryan Guest

    You'll need to flush the std{out|err} or set them unbuffered; or you can
    just forget about relying on std{out|err} being ordered per write-order.
    Lie Ryan, Dec 18, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.