简体   繁体   English

已打开为非阻塞的管道上的Python readline

[英]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. 我有一个以非阻塞模式打开的Linux fifo。 As expected, when I call read on the file object, it returns immediately. 正如预期的那样,当我调用文件对象上的read时,它会立即返回。 I use select to make sure there is no busy waiting, but that my program is still notified when there is any data available. 我使用select来确保没有忙碌的等待,但是当有任何数据可用时我的程序仍会收到通知。 Out of curiosity, I tried the readline function and was surprised to find that readline blocks until a newline character is found. 出于好奇,我尝试了readline函数,并惊讶地发现readline阻塞直到找到换行符。 I examined the processor usage via top and it seems like readline does not busy wait. 我通过top检查了处理器的使用情况,看起来readline并不忙等待。 Since my application is performance sensitive, I am wondering if there are any performance implications when using readline on a non-blocking socket. 由于我的应用程序对性能敏感,我想知道在非阻塞套接字上使用readline时是否存在性能影响。

The core part of Python's readline is in fileobject.c:get_line and is Python的readline的核心部分是在fileobject.c:get_line和

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? 你确定你的观察结果是你认为的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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