Hi Iker, I was juggling with a not dissimilar problem today. Trying to get some stdout from the telnet lib to flush to file. I just cam across this post which did the trick for me: http://www.gossamer-threads.com/lists/python/python/658167 Basically the idea is that you reopen the sys.stdout file descriptor but set it to unbuffered for writing
Rob Message quoted below: *Re: Unbuffered stdout/auto-flush* Remove Highlighting<http://www.gossamer-threads.com/lists/python/python/658219?nohighlight=1#658219>[In reply to <http://www.gossamer-threads.com/lists/python/python/658167#658167>] ------------------------------ On Jun 21, 12:29 pm, Yang Zhang <[email protected]> wrote: > Hi, is there any way to get unbuffered *stdout*/stderr without relying on > the -u flag to *python* or calling .*flush*() on each print (including > indirect hacks like replacing sys.*stdout* with a wrapper that succeeds > each write() with a *flush*())? Thanks in advance! > I think the only way is to reopen the *stdout* file descriptor: import sys import os # reopen *stdout* file descriptor with write mode # and 0 as the buffer size (unbuffered) sys.*stdout* = os.fdopen(sys.*stdout*.fileno(), 'w', 0) print "unbuffered text" Sebastian -- http://mail.*python*.org/mailman/listinfo/*python*-list<http://mail.python.org/mailman/listinfo/python-list> On Tue, Sep 15, 2009 at 11:46 AM, JammyZ <[email protected]> wrote: > Hi, > thanks for the help. I followed your example and got it solved in no > time. > > Iker. > > 2009/9/15 Michael Twomey <[email protected]> > > >> AFAIK stdout/stderr semantics vary slightly across platforms so you >> are going to run into issues like this when redirecting output, >> particularly on windows and linux. >> >> Since you are using subprocess you can retain control of the output. >> >> e.g. you can redirect the output into a file object via the stdout and >> stderr kwargs: >> >> output_log = open("output.log", "ab") >> check_call(['net', 'stop', 'Apache Tomcat 6'], stdout=output_log, >> stderr=output_log) >> >> Alternatively you can use subprocess.PIPE to capture the output and >> manage it in your code (since you are using check_call I assume you >> are blocking awaiting output anyway). This is probably the preferable >> solution. >> >> PS the flush() requirement probably comes from the fact your print >> output is buffered, and that you are running the processes without >> capturing their stdout/stderr, which means that it's bit of a crap >> shoot to see who flushes their buffer first (bet the short running >> process always wins). >> >> mick >> >> On Mon, Sep 14, 2009 at 18:40, JammyZ <[email protected]> wrote: >> > Hi all, >> > I have a 2.6 script here that works OK on Linux, I'm porting it to >> > Windows and I cannot get all the output correctly being added to the log >> > file. >> > >> > Linux version runs like this: >> > /opt/www/auto-update.py > /opt/www/autoupdate.log 2>&1 >> > >> > Windows version: >> > auto-update.py >autoupdate.log 2>&1 >> > >> > The issue is with the output generated by subprocesses, which I execute >> like >> > this: >> > check_call(['net', 'stop', 'Apache Tomcat 6']) >> > >> > Nothing of the output of the previous command ends up in the log file, >> > output of print commands is written to the log file correctly though. >> > >> > I had an issue on Linux, where output would come out of order so I had >> to >> > run sys.stdout.flush after every print statement. >> > >> > If I run it from the command line everything gets displayed. >> > >> > Any idea? >> > >> > Cheers, >> > Iker. >> > >> > > >> > >> >> >> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Python Ireland" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.ie/group/pythonireland?hl=en -~----------~----~----~----~------~----~------~--~---
