`patch_stdout` lines printed out of order
See original GitHub issueLines printed from an inputhook
might be printed out of order when doing patch_stdout
.
Example to illustrate:
from __future__ import unicode_literals
from prompt_toolkit.shortcuts import prompt, create_eventloop
from time import sleep
x = 0
def inputhook(context):
global x
print('%d before sleep' % x)
x = x + 1
sleep(1)
print('%d after sleep' % x)
x = x + 1
prompt('> ',
eventloop=create_eventloop(inputhook=inputhook),
patch_stdout=True)
Example output (the order will be different between runs):
0 before sleep
1 after sleep
2 before sleep
3 after sleep
5 after sleep
4 before sleep
Expected numbers to be 0, 1, 2, 3, 4, 5, but instead they are 0, 1, 2, 3, 5, 4.
Running MacOS 10.11.6 and iterm2 3.0.15.
Observations so far:
• the behavior is the same on python 2.7 and python 3.5
• the behavior is (unsurprisingly) the same even if use sys.stdout.write
and sys.stdout.flush()
instead of print
, however, if I write
without a newline, lines are in order.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
python - Correcting out of order printing from stream redirection
1 Answer 1 · stdin is always buffered · stderr is always unbuffered · if stdout is a terminal then buffering is automatically...
Read more >How to Overwrite the Previous Print to Stdout in Python?
Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ( '\r' ) character within...
Read more >cabal test outputs stdout and stderr out of order #3024 - GitHub
I expected cabal test to produce text output in the same order as running the executable directly, even if the test executable outputs...
Read more >Mocking print() in Unit Tests - Real Python
In this lesson, I'm going to talk about how to mock print() if you're testing ... def greet(name): print('Hello ', name) from unittest.mock...
Read more >Python | Testing Output to stdout - GeeksforGeeks
In order to test that output is actually getting there, it is to be mocked out using a ... How to print to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seeing this as well, on MacOS and Linux, appears to be caused by randomization of high priority tasks in eventloop.PosixEventLoop.run:
For the time being, I’m working around this by monkey-patching _StdoutProxy’s _do method to call run_in_terminal directly instead of passing it to eventloop.call_from_executor - but I’m not sure whether this may cause any undesirable side effects.
Wow, interesting… This should be a bug. Thanks for reporting and the code example! That’s very helpful.