简体   繁体   中英

Python readline on a pipe that has been opened as non-blocking

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM