question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`patch_stdout` lines printed out of order

See original GitHub issue

Lines 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
atherracommented, Jun 16, 2017

Seeing this as well, on MacOS and Linux, appears to be caused by randomization of high priority tasks in eventloop.PosixEventLoop.run:

random.shuffle(tasks)
random.shuffle(low_priority_tasks)

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.

1reaction
jonathanslenderscommented, May 21, 2017

Wow, interesting… This should be a bug. Thanks for reporting and the code example! That’s very helpful.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found