Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subprocess.Popen and ordering writes to stdout and stderr

Reply
Thread Tools

subprocess.Popen and ordering writes to stdout and stderr

 
 
Chris Withers
Guest
Posts: n/a
 
      12-17-2009
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):

@tempdir()
def test_out_and_err(self,d):
path = d.write('test.py','\n'.join((
"import sys",
"sys.stdout.write('stdout\\n')",
"sys.stderr.write('stderr\\n')",
"sys.stdout.write('stdout2\\n')",
)),path=True)
compare('stdout\nstderr\nstdout2\n',
execute(sys.executable+' '+path))

....because:

AssertionError:
@@ -1,4 +1,4 @@
-stdout
-stderr
-stdout2
+stdout
+stdout2
+stderr

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

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      12-18-2009
On 12/18/2009 8:15 AM, Chris Withers wrote:
>
> ....the order of the writes isn't preserved.
> How can I get this to be the case?
>


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Z-Ordering (Morton ordering) question nbigaouette C Programming 2 11-06-2009 05:26 AM
stdout and stderr don't occur sequentially in Eclipse and Netbeans ZelluX Java 15 06-25-2008 12:06 AM
Speeding up writes to STDOUT enjoylife_95135@hotmail.com Perl Misc 7 06-06-2006 01:51 AM
Re: embedding Python and changing stdin, stdout and stderr Vincent Touquet Python 0 09-06-2004 04:35 PM
embedding Python and changing stdin, stdout and stderr Vincent Touquet Python 1 09-03-2004 12:25 PM



Advertisments