Python readline on pipe that was open as non-blocking

I have a Linux fifo that was open in non-blocking mode. As expected, when calling read on a file object, it returns immediately. I use select to make sure the wait is not busy, but my program is still notified of the availability of data. Out of curiosity, I tried the readline function and was surprised to find that the reading blocks remain until a newline character is found. I reviewed CPU usage from above and it seems that readline is not busy. Since my application is performance sensitive, I wonder if there are performance implications when using readline on a non-blocking socket.

+3
source share
1 answer

The bulk of the Python readline is located in the .c: get_line file object and

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

Nothing special happens there. Are you sure that your observations are what you think?

+1
source

Source: https://habr.com/ru/post/1729070/


All Articles