开发者

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

开发者 https://www.devze.com 2022-12-17 00:54 出处:网络
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

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 t开发者_如何学Pythonhe 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消