LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2018, 05:33 PM   #1
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
What is the Pythonic equivalent to Perl's "$| = 1" ?


I'm pretty sure that the subject line pretty much is all you need, but just in case:

I have a Python (v3.4.5) that runs for a good long time and I've included status messages that I'd like to see displayed during execution that wind up not being displayed until some time when the system sees that you have a buffer full of text to display. So instead of seeing
Code:
Beginning data collection phase... (20 seconds pass)  Done. (Took 00:20)
I see
Code:
(20 seconds pass)
Beginning data collection phase...  Done. (Took 00:20)
(Actually, it's worse than that. I might see a couple of more "Beginning... Done" messages before the OS buffering decides to finally spit text onto the terminal.)

In ages past, we'd do something like this in our C programs:
Code:
printf( format, args);
fflush(STDOUT);
to get immediate output. In Perl, I can control this with "$| = 1" near the top of the script which causes immediate output rather than letting Perl and the OS wait until there was a buffer full of stuff to display.

Surely Python has a mechanism to force this without having to write a wrapper around prints that (somehow) does a manual flush. Right?

TIA for any tips.

UPDATE: I'm not sure how I missed this when I was poking around in the Python docs but there is a command line switch that is supposed to do, basically, what Perl's "$| = 1" statement does. I've modified my "shebang" line to be:
Code:
#!/usr/bin/python3 -u
Even after that, though, I haven't been able to make it work in my testing. Still plugging away on this.

Last edited by rnturn; 07-15-2018 at 05:57 PM.
 
Old 07-14-2018, 05:37 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Code:
sys.stdout.flush()
Example:

https://www.linuxquestions.org/quest...5/#post5350284

Last edited by dugan; 07-14-2018 at 05:43 PM.
 
Old 07-14-2018, 05:44 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
See here for various options:

https://stackoverflow.com/questions/...print-function
 
Old 07-15-2018, 07:06 PM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by hydrurga View Post
Thanks...

One of the suggestions at that link did the trick though it seems like it's going to be a PITA to have to have to add "flush=True" to every intermediate print that's showing status when one global switch could (should, IMHO) have done the trick. BTW, my testing involving the "-u' command line switch only seemed to work with Python 2.7---anything I tried with Python3 was a bust...except the "flush=". Maybe that got fixed in 3.4.6 and later. My test script is/was:
Code:
#!/usr/bin/python3 -u

import time

def current_tod():
    return time.strftime("%H:%M:%S")

print("Starting at %s ..." % current_tod(), end=" ", flush=True)
time.sleep(5)
print("Finished at %s." % current_tod())
Remove the "flush=" parameter and nothing is seen until the "Finished" message is displayed. For now, that "-u" switch seems to be nothing but a nop.

Now I'm off to doing python3<-->psql testing. Should be, uh, "fun". That's it "fun".
 
Old 07-15-2018, 07:31 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,118

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You can't use stdbuf ?
 
Old 07-15-2018, 08:01 PM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by dugan View Post
Code:
sys.stdout.flush()
OK... that'd work but having to go that route is what I was hoping to avoid. I can think of a case where I may employ that when I translate some module I've written for Perl to a Python equivalent. Using those lower level functions really cries out for enclosing them in a black box-type module where the gorier code is hidden. I'd hate to be sprinkling all the "sys.*" function calls in higher level code. To me, anyway, it hinders understanding the processing flow---sort of like dropping into inline assembler.
 
Old 07-16-2018, 04:21 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,772

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
what you want is: unbuffered write (I think), probably this helps: https://stackoverflow.com/questions/...in-the-program
 
Old 07-16-2018, 09:49 AM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by pan64 View Post
what you want is: unbuffered write (I think), probably this helps: https://stackoverflow.com/questions/...in-the-program
Which, from my reading of the manpage, is what the "-u" switch was supported to provide so that you wouldn't have to include all this extra verbiage in each and every print() invocation. I'm wondering if the "-u" switch isn't being deprecated which force all these extras to be added to each use of print(). Just my HO but this is the sort of thing that makes the language ugly. I'm still learning Python (dabbled with it for some time but really digging in now) but from my limited experience, the way I/O gets done in Python is crazy: sys.*, io.* who-knows-what-else.*.
 
Old 07-16-2018, 11:34 AM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
One of Python's design principles was "explicit is better than implicit".
 
Old 07-17-2018, 12:53 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,772

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
the unbuffered i/o is much slower. That's why we use buffers.
But in the example I sent you can reopen stdout unbuffered, therefore you do not need to modify your print statements.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Puppy Linux latest version (or equivalent) of "wary" and "precise retro" versions? madscijr Linux - Newbie 12 11-13-2017 06:54 PM
[SOLVED] Equivalent to Perl's "=~" Operator? mrm5102 Programming 3 07-25-2012 08:21 AM
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 09:30 PM
Is there an "apt-get" equivalent of "yum list available"? geeeeky.girl Linux - General 3 07-20-2008 08:40 PM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 04:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration