views:

352

answers:

1

I have a Linux fifo that has been opened in non-blocking mode. As expected, when I call read on the file object, it returns immediately. I use select to make sure there is no busy waiting, but that my program is still notified when there is any data available. Out of curiosity, I tried the readline function and was surprised to find that readline blocks until a newline character is found. I examined the processor usage via top and it seems like readline does not busy wait. Since my application is performance sensitive, I am wondering if there are any performance implications when using readline on a non-blocking socket.

+1  A: 

The core part of Python's readline is in fileobject.c:get_line and is

FILE_BEGIN_ALLOW_THREADS(f)
FLOCKFILE(fp);

while ((c = GETC(fp)) != EOF &&
       (*buf++ = c) != '\n' &&
       buf != end)
       ;
FUNLOCKFILE(fp);
FILE_END_ALLOW_THREADS(f)

where

#ifdef HAVE_GETC_UNLOCKED
#define GETC(f) getc_unlocked(f)
#define FLOCKFILE(f) flockfile(f)
#define FUNLOCKFILE(f) funlockfile(f)
#else
#define GETC(f) getc(f)
#define FLOCKFILE(f)
#define FUNLOCKFILE(f)
#endif

There's nothing special going on. Are you sure your observations are what you think they are?

Andrew Dalke
Thanks for digging out this code snippet for me. With respect to the code you posted, my question is whether the GETC(fp) is returning immediately with no data if the fp is non-blocking, which means the while loop would be iterating rapidly and wasting CPU cycles.
BrainCore
I don't know what C does for this case. Best would be if you could post a reproducible and/or double check your observations. I would assume it busy-waits, but you reported that it doesn't.
Andrew Dalke