poll/epoll 会阻塞吗?它与异步IO有什么不同?

发布于 2024-12-01 09:37:43 字数 261 浏览 2 评论 0原文

我一直认为 poll/epoll 不会阻塞。这就是 Nginx 等非阻塞服务器使用它们的原因。

但在这个Stackoverflow问题中,多次提到了轮询阻塞。

那么poll/epoll会阻塞吗?

poll/epoll 与 async IO 有何不同?

I was always under the impression that poll/epoll doesn't block. That's why they are used by non-blocking servers such as Nginx.

But in this Stackoverflow question it was stated several times that poll blocks.

So does poll/epoll block?

And how is poll/epoll different from async IO?

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

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

发布评论

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

评论(1

梦归所梦 2024-12-08 09:37:43

是的,poll/epoll 块。分派线程来服务客户端的服务器通常无法像使用 epoll 等 I/O 事件通知模型的服务器那样扩展。 poll 比 epoll 更旧且效率更低(O(n) vs O(1))。

[更新]

Nginx 不是非阻塞的。当请求到来时,epoll_wait 正在等待的事件之一会收到通知,并且对 epoll_wait 的调用将返回。然后 Nginx 循环遍历为每个事件提供服务的信号事件。 Nginx 源代码可以在这里找到... http://nginx.org/download/nginx-1.1。 1.tar.gz

看一下 nginx-1.1.1\src\event\modules\ngx_epoll_module.c 中的 ngx_epoll_process_events 函数

[UPDATE2]

另请参阅epoll_wait(2) 的手册页 ... http://linux.die.net/man/2/epoll_wait

#include <sys/epoll.h>
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

指定超时为-1会使epoll_wait(2)无限期等待,
当指定超时为零时,epoll_wait(2) 返回
即使没有可用的事件也立即(返回代码等于
零)。

[UPDATE3]

为了向自己证明 Nginx / epoll 会阻塞,请在 Linux 上尝试这个...

  1. 下载源代码并将
  2. cd 解压到源目录
  3. ./configure --with-debug (注意:我有添加 libpcre3-dev)
  4. make
  5. sudo make install
  6. 要启动 nginx: /usr/local/nginx/sbin/nginx (注意:我有杀死阿帕奇首先 sudo /etc/init.d/apache2 stop)
  7. sudo gdb
  8. 文件 /usr/local/nginx/sbin/nginx
  9. b ngx_epoll_module.c:531(设置断点)
  10. 在另一个终端窗口中,ps -ef | grep nginx 并在 gdb 中使用 nginx 工作进程(不是主进程)的 PI​​D
  11. attach
  12. 继续 恢复process

您可能需要继续几次,但它最终应该会阻塞。然后打开浏览器并转到 http://localhost ...调试器应该在 epoll_wait 返回后立即中断。

Yes, poll/epoll block. Servers that spin off threads to service clients typically don't scale as well as servers that use an I/O event notification model like epoll. poll is older and less efficient than epoll (O(n) vs O(1)).

[UPDATE]

Nginx is not non-blocking. When a request comes in, one of the events epoll_wait is waiting for is notified and the call to epoll_wait returns. Then Nginx loops through the signaled events servicing each one. The Nginx source code is available here ... http://nginx.org/download/nginx-1.1.1.tar.gz

Take a look at the ngx_epoll_process_events function in nginx-1.1.1\src\event\modules\ngx_epoll_module.c

[UPDATE2]

See also the man page for epoll_wait(2) ... http://linux.die.net/man/2/epoll_wait

#include <sys/epoll.h>
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

Specifying a timeout of -1 makes epoll_wait(2) wait indefinitely,
while specifying a timeout equal to zero makes epoll_wait(2) to return
immediately even if no events are available (return code equal to
zero).

[UPDATE3]

To prove to yourself that Nginx / epoll blocks, try this on Linux...

  1. Download the source and unarchive
  2. cd to the source directory
  3. ./configure --with-debug (NOTE: I had to add libpcre3-dev)
  4. make
  5. sudo make install
  6. To start nginx: /usr/local/nginx/sbin/nginx (NOTE: I had to kill apache first sudo /etc/init.d/apache2 stop)
  7. sudo gdb
  8. file /usr/local/nginx/sbin/nginx
  9. b ngx_epoll_module.c:531 (to set a break point)
  10. In another terminal window, ps -ef | grep nginx and use the PID of the nginx worker process (not the master)
  11. back in gdb, attach <PID of nginx worker>
  12. continue to resume the process

You may have to continue a couple times but it should eventually block. Then open a browser and go to http://localhost ... the debugger should then break right after epoll_wait returns.

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