了解libuv/epoll/非阻塞网络IO [英] Understanding libuv / epoll / non-blocking network IO
问题描述
我试图了解 Node.js
/ libuv
中的非阻塞网络IO如何工作.我已经发现 file IO是使用 libuv
工作线程完成的(因此,在后台线程中).但是,在不同地方都说网络 IO是通过使用 epoll
, kqueue
等系统调用以非阻塞方式完成的(取决于在操作系统上).
I am trying to understand how non-blocking network IO is working in Node.js
/libuv
. I already found out that file IO is done using libuv
worker threads (thus, in a background thread). However it is stated in various places that network IO is done in a non-blocking fashion using system calls like epoll
, kqueue
, etc (depending on operating system).
现在,我想知道这是否意味着实际的IO部分( read()
)是在主线程上完成 still 并因此阻塞了,即使e.G.使用 epoll
?据我了解, epoll
仅通知可用事件,而实际上不进行读/写.至少在我发现的示例中(例如 http://davmac.org/davpage/linux/async-io.html ) epoll
总是与 read
系统调用结合使用,这是一个阻塞的IO操作.
Now I am wondering if this means that the actual IO part (read()
) is still done on the mainthread, and thus blocking, even if e. g. epoll
is used? As for my understanding, epoll
only notifies about available events, but does not actually do the read/write. At least in the examples I found (e. g. http://davmac.org/davpage/linux/async-io.html) epoll
is always used in combination with the read
system call, which is a blocking IO operation.
换句话说,如果 libuv
使用的是单线程,而 epoll
,则在有数据可读取时发出通知,然后执行以下读取操作在主线程上运行,从而有可能阻止在主线程上进行其他操作(网络请求的思考)?
In other words, if libuv
is using a single thread and epoll
, to have a notification when data is available to read, is the then following read operation beeing executed on the mainthread and thus potentially blocking other operations (thinking of network requests) on the mainthread?
推荐答案
引用文件的文件描述符始终被报告为准备好供 epoll/poll/select
读/写,但是,读/写
可能会阻止等待数据被读/写.这就是为什么文件I/O必须在单独的线程中完成的原因.
File descriptors referring to files are always reported as ready for read/write by epoll/poll/select
, however, read/write
may block waiting for data to be read/written. This is why file I/O must be done in a separate thread.
具有管道和套接字的非阻塞 send/recv
确实是非阻塞的,因此可以在I/O线程中完成而没有阻塞线程的风险.
Whereas non-blocking send/recv
with pipes and sockets are truly non-blocking and hence can be done in the I/O thread without risk of blocking the thread.
这篇关于了解libuv/epoll/非阻塞网络IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!