简体   繁体   English

了解libuv / epoll /非阻塞网络IO

[英]Understanding libuv / epoll / non-blocking network IO

I am trying to understand how non-blocking network IO is working in Node.js / libuv . 我试图了解Node.js / libuv非阻塞网络IO的工作libuv I already found out that file IO is done using libuv worker threads (thus, in a background thread). 我已经发现文件 IO是使用libuv工作线程完成的(因此,在后台线程中完成)。 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是通过使用epollkqueue等系统调用(取决于操作系统)以非阻塞方式完成的。

Now I am wondering if this means that the actual IO part ( read() ) is still done on the mainthread, and thus blocking, even if eg epoll is used? 现在,我想知道这是否意味着即使在使用了epoll情况下,实际的IO部分( read()仍在主线程上完成并因此阻塞了? As for my understanding, epoll only notifies about available events, but does not actually do the read/write. 据我了解, epoll仅通知可用事件,而实际上不进行读/写。 At least in the examples I found (eg 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. 至少在我发现的例子(例如http://davmac.org/davpage/linux/async-io.htmlepoll始终与组合使用read系统调用,这是阻塞IO操作。

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? 换句话说,如果libuv使用单个线程和epoll来在有数据可供读取时发出通知,则在主线程上执行随后的读取操作beeing,从而潜在地阻止了该线程上的其他操作(网络请求的思考)主线程?

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. 始终通过epoll/poll/select将指向文件的文件描述符报告为已准备好进行读/写,但是, read/write可能会阻止等待数据被读/写。 This is why file I/O must be done in a separate thread. 这就是为什么文件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. 带有管道和套接字的非阻塞send/recv实际上是非阻塞的,因此可以在I / O线程中完成而不会阻塞线程。

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

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