已以非阻塞方式打开的管道上的 Python readline

发布于 2024-08-18 00:42:06 字数 249 浏览 3 评论 0原文

我有一个以非阻塞模式打开的 Linux fifo。正如预期的那样,当我对文件对象调用 read 时,它会立即返回。我使用 select 来确保没有忙等待,但当有任何可用数据时我的程序仍然会收到通知。出于好奇,我尝试了 readline 函数,并惊讶地发现 readline 会阻塞,直到找到换行符。我通过 top 检查了处理器的使用情况,看起来 readline 并不忙等待。由于我的应用程序对性能敏感,因此我想知道在非阻塞套接字上使用 readline 时是否会对性能产生任何影响。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2024-08-25 00:42:06

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)

没有

#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

什么特别的事情发生。你确定你的观察结果就是你所认为的那样吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文