osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

printing to stdout


On Fri, 17 Aug 2018 08:31:22 +1000
Cameron Simpson <cs at cskk.id.au> wrote:

> This isn't specific to Python, you'll find it with most programmes.
> (The shell's builtin "echo" command is an exception.)

[buffer explanation]

I already suspectec a buffered output and to check if it was the
buffer, I created a lot of output by pressing the button that generates
an INT quite a lot of times. But apparently this wasn't enough. After
all, the output is small:

-------
38: ff
39: ff
3a: ff
3b: fb


I fear I generated 4095 bytes instead of 4096. Some wise human being
once said "Murphy was an optimist" :)

> So let's look at your script:
> 
> >      print ("%x: %x" % (pcf, output))
> [...]
> >          print ('[ALERT] possible INT loop, disable 10 seconds')
> 
> Your programme will be writing into a buffer. Your messages only go
> out when enough have accrued to fill the buffer.
> 
> To force te messages to go out in a timely manner you need to flush
> the buffer. You have two choices here: call sys.stdout.flush() or
> pass "flush=True" with the print call, eg:
> 
>   print(...., flush=True)
> 
> Just looking at your loop I would be inclined to just call flush once
> at the bottom, _before_ the sleep() call:
> 
>   sys.stdout.flush()
> 
> Your call; the performance difference will be small, so it tends to
> come down to keeping your code readable and maintainable.

Yep, the "sys.stdout.flush()" did the job :) I had already been mucking
about with file=sys.stderr, but without result, just because of the
fact that stderr is never buffered AFAIK (the supervised script "run"
has an "exec 2>&1")

Anyroad, it works :)

Thnx!

-- 
richard lucassen
http://contact.xaq.nl/