Skip to content

patch_stdout lines printed out of order #507

@johshoff

Description

@johshoff

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions