[SOLVED] python output using "python script > result" shows that the sequence of printing is haphazard
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
python output using "python script > result" shows that the sequence of printing is haphazard
I have a python script "script.py".
When I run "python script.py" the output on the screen seems to be in sequence.
But if I direct the output to a file via
"python script.py > a-file", the sequence is definitely messed up.
How to preserve the sequence to allow debugging?
The printings from script.py are from print("..."), os.system("cat a-list"),
and os.system("a-Fortran-binary"), where a-Fortran-binary printing output to the screen.
$ make
gfortran fort.f90 -o fortbin
python test.py
L is [1, 2, 3]
L is 1 2 3
I am in Fortran!
echo "----separator----"
----separator----
python test.py > savepy-output
cat savepy-output
L is 1 2 3
I am in Fortran!
L is [1, 2, 3]
See that `L is [1, 2, 3]' appears on the first line in first instance and
the third line in the second instance.
Probably python is not designed to do this kind of things.
That's not a great example - could have been simplified further to highlight the issue.
Anyway, the docs are a bit vague, but it appears to be because os.system is asynchronous, and you need an explicit os.wait() call to change that.
Or you could replace it with subprocess.call, which will: "Run the command described by args. Wait for command to complete, then return the returncode attribute."
1. add flush=True so that we have print(any_string,flush=True)
2. Option 1 gets a bit tiring since we have to put flush=True in every single print statement.
To avoid it, ask python not to buffer using "-u" option.
So we need to do "python -u script.py"
option2 seems better since it involves the least of work but we have to remember to use -u.
As per "python --help" instead of passing "-u" every time you can set an environment variable "PYTHONUNBUFFERED=x"; put it in a startup file and you don't need to remember it.
Not sure this is related to your question though, unless you're saying that preventing buffering has the effect of enforcing atomicity in the os.system calls?
1. add flush=True so that we have print(any_string,flush=True)
2. Option 1 gets a bit tiring since we have to put flush=True in every single print statement.
To avoid it, ask python not to buffer using "-u" option.
So we need to do "python -u script.py"
option2 seems better since it involves the least of work but we have to remember to use -u.
In case of asynchronous calls non of these options are correct: the order of the execution is unpredictable. Adding flush=True or -u will not change it at all.
But os.system itself is not an async call, it will use wait for the end of the execution, so only these buffers caused this issue (that's why it helped in this case).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.