如果 Web 服务器是非阻塞的,这是否意味着它处理 IO 的方式与 node.js 相同? [英] If a web server is non-blocking, does this mean it is handling IO the same as node.js?

查看:89
本文介绍了如果 Web 服务器是非阻塞的,这是否意味着它处理 IO 的方式与 node.js 相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很快就会使用名为 Undertow 的服务器.网站 说:

I will soon be using a server named Undertow. The website says:

Undertow 是一个用 Java 编写的灵活的高性能 Web 服务器,提供基于 NIO 的阻塞和非阻塞 API

Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO

如果 Undertow 允许非阻塞,那和 node.js 一样吗?我指的不是语言或类似的东西.我有一个单独的项目,我认为 node.js 是一个不错的选择,但如果我可以将一个产品用于多个项目,那将会很有帮助.

If Undertow allows non-blocking, is that the same as node.js? I don't mean the languages or anything like that. I have a separate project where I thought node.js would have been a good choice, but if I can use a single product for multiple projects it would be helpful.

我发现了这个问题.Java NIO 非阻塞模式 vs node.js 异步操作而且我开始觉得我把事情搞糊涂了.

I found this question. Java NIO non-blocking mode vs node.js asychronous operation And I am starting to think I have confused things.

推荐答案

Undertow 基于 JBoss XNIO 库,与 Nodejs 一样,XNIO 依赖于操作系统功能(epoll 或 kqueue(如果可用)以收到 IO 事件通知(例如,当数据可从套接字读取时).

Undertow is based on the JBoss XNIO library and like Nodejs, XNIO relies on operating system capabilities (epoll or kqueue when available) to be notified for IO events (when data is available to read from a socket for example).

在 Undertow 中,通过 IO 线程 遵循此模型来接受传入请求.在这些线程上执行阻塞操作意味着延迟新传入请求的处理.请参阅 Undertow 的文档在 IO 线程上

In Undertow, accepting incoming requests is done following this model, by the IO threads. Doing blocking operations on these threads would mean delaying the handling of new incoming requests. See Undertow's documentation on IO threads

在 IO 线程旁边,Undertow 管理另一个线程池,Worker 线程,用于处理阻塞任务(想想调用 Web 服务或查询数据库之类的任务.)而这是你不会做的使用 Nodejs

Next to the IO threads, Undertow manages another thread pool, the Worker threads, to handle blocking tasks (think of tasks like calling a webservices or querying a database.) And this is what you wont get with Nodejs!

要使用工作线程,必须从 IO 线程分派请求处理.该 API 全面且易于使用,再次请参阅 Undertow 的文档,作为起点.

To use a Worker thread, the request handling has to be dispatched from the IO thread. The API is comprehensive and easy to use, again, see Undertow's documentation, as a starting point.

这篇关于如果 Web 服务器是非阻塞的,这是否意味着它处理 IO 的方式与 node.js 相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆