iostream._newbuffer starts auto-flush clock #2335

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

@minrk
Member
minrk commented Aug 24, 2012

This means that the timer runs between flush calls, rather than the previous logic where the timer started on the first print after each flush. The problem with the previous logic was that it meant the first message after any flush could never be displayed immediately without an explicit manual stdout.flush().

The disadvantage of the new approach is that most cells with two or more print statements will result in at least two stdout messages, because when used interactively the first print statement will ~always trigger the flush_timeout logic.

@minrk minrk iostream._newbuffer starts auto-flush clock
This means that the timer runs between flush calls, rather than the previous logic where the timer started on the first print after each flush.  The problem with the previous logic was that it meant the first message after any flush could *never* be displayed immediately without an explicit manual `stdout.flush()`.

The disadvantage of the new approach is that most cells with two or more print statements will result in at least two stdout messages, because when used interactively the first print statement will ~always trigger the flush_timeout logic.
e2e91f9
@travisbot

This pull request fails (merged e2e91f9 into 144f08a).

@Carreau
Member
Carreau commented Sep 3, 2012

For me it just pushes the problem further:

import time
print 'a'
print 'b'
time.sleep(3)

result in a, 3 second delay, ( b and return )

@bfroehle
Contributor
bfroehle commented Sep 3, 2012

The failing test shows that print('hi') turns into write('hi'); write('\n'), only the first of which gets sent over the wire immediately.

@Carreau
Member
Carreau commented Sep 3, 2012

One solution I see is to start a timer that trigger an event to flush after flush_interval seconds if flush has not been called, but I don't see any good way to do it.

@bfroehle
Contributor
bfroehle commented Sep 3, 2012

Unless we want to add another thread for monitoring the io streams, I don't
think that it would be possible.

@minrk
Member
minrk commented Sep 3, 2012

I don't think a thread can work, since the sockets themselves are not threadsafe. The only way I see to guarantee that print statements will always flush is if we send on every write instead of flush. The obvious (and I think unacceptable) disadvantage of that would be that 100 print statements in a row would invariably result in 100 messages, incurring potentially enormous overhead.

This PR doesn't resolve the whole issue, but it does strictly improve the behavior.

@minrk
Member
minrk commented Sep 3, 2012

Alternatively, we could have an 'unbuffered' mode, like python -u, which enables the extraordinarily inefficient 'write implies flush' behavior.

In all, I don't think we can have perfect behavior without immensely wasteful overhead. Thinking about it, I'm not even sure this PR really does improve the situation (though it does improve it in most annoying cases that I have encountered in practice).

Taking the relatively generic example of:

for i in range(5):
    print 'A'
    print 'B'
    print 'C'
    time.sleep(1)

Both before and after this PR, flushes are only triggered on print 'A', resulting in batches of BCA arriving together. The difference being that with this PR, the first 'A' will be printed if it has been long enough since the last flush, whereas before this PR, it is guaranteed that nothing will be displayed until the second iteration in the loop.

@Carreau
Member
Carreau commented Sep 8, 2012

@minrk , do you want to fix the failing test and merge this, or we forget it for now ?

@Carreau
Member
Carreau commented Sep 30, 2012

Pinging the status of this PR.

@minrk
Member
minrk commented Nov 7, 2012

closing this, I don't think it helps. Someone can look into 'unbuffered mode' if they feel like it.

@minrk minrk closed this Nov 7, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment