Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

javascript - Confusion about node.js internal asynchronous I/O mechanism

  1. I have learned that node.js use libeio internally to perform async file I/O, with thread pool, on *nix platform, am I right?
  2. What about async network I/O? Is it done by libev? Is there also a thread pool?
  3. If there is thread pool inside, how could it be more efficient than traditional one-thread-per-request model? And is it one thread per I/O request?
  4. And what's the mechanism on windows? I know it's done by IOCP, and there's a kernel level thread pool, right?
  5. Why linux doesn't have a native completely AIO mechanism like windows IOCP yet? Will it have in future?

Update according to changchang's answer:

  1. I took a quick view at the source code @changchang have given, found that the default thread pool size can be reset by UV_THREADPOOL_SIZE, I'm wondering in which case this will be used?
  2. I also found getaddrinfo use this thread pool, is there any more except fs? And if all sync jobs will be done in this thread pool, is the default size '4' enough?
  3. As my understanding now, there will be 6 basic threads in node.js process: 1 V8 thread(event loop, where user javascript codes runs), 1 libuv event loop, and 4 in thread pool, am I right?
  4. And how can I see these threads in my shell(Ubuntu)? I use ps -eLf | grep node | grep -v grep only saw two:

    root 16148 7492 16148 0 2 20:43 pts/26 00:00:00 ./bin/node /home/aaron/workspace/test.js
    root 16148 7492 16149 0 2 20:43 pts/26 00:00:00 ./bin/node /home/aaron/workspace/test.js

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. First of all, libuv has removed the libeio from it. But it does perform async file I/O with a thread pool like libeio just as you mentioned.

  2. libuv also removes libev. It does the async network I/O based on the async I/O interfaces in different platforms, such as epoll, kqueue and IOCP, without a thread pool. There is a event loop which runs on the main thread of uv which polls the I/O events and processes them.

  3. The thread pool inside libuv is a fixed size thread pool (4 in uinx like system). It performs a task queue role and avoids the exhaustion of the system resources by generating threads indefinitely when the requests increase.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...