!! Non blocking IO !!

Stephan Houben stephan at pcrm.win.tue.nl
Wed Sep 29 11:31:34 EDT 1999


On Wed, 29 Sep 1999 14:47:07 +0200, Pierre Saloni <me at nospam.com> wrote:
>Hello,
>
>    I would like to know how to use a non bloking IO, like print.
>    In perl I would have used $|=0 to set the non blockng mode.
>    What should I use in python ?
>

I don't know what you mean with "non-blocking", but according to the
Perl manpage, the $| variable determines whether to flush the file
after every output operation.

$| = 0 means do NOT flush after every operation, and this is the default.
Actually, that's also the way things work in Python; you have to flush
the buffers explicitely. Use the flush() method on the file object if you
want the $| = 1 effect.

There's one caveat, which might have triggered the question: the Python
interpreter flushes (at least) stdout just before printing the prompt.

So if you print just a single character 'a' with:
print 'a',
then you will see it appear immediately.

However, if you do:
import time
print 'a', ; time.sleep(1)

, then you will note that it takes one second before 'a' is printed;
in other words, stdout is flushed right before printing the prompt, not
right after the `print' statement.

Hope this helps,

Stephan 




More information about the Python-list mailing list