Relationship between event loop,libuv and v8 engine

谁说我不能喝 提交于 2019-12-04 10:53:02

问题


I am learning through the architecture of Node.js. I have following questions.

  1. Is event loop a part of libuv or v8?
  2. Is event queue a part of event loop? are event queue generated by libuv or v8 engine or event loop itself?
  3. What is the connection between libuv and v8 engine?
  4. If event loop is single threaded, does libuv come into picture to create multiple threads to handle File I/O?
  5. Does browsers have event loop mechanism or just Node.js does?

回答1:


  1. The event loop is, first and foremost, a high-level concept that's a fundamental part of the JavaScript programming model. Practically, every V8 embedder needs to implement an event loop. V8 provides a default implementation, which embedders can replace or extend.

  2. I don't understand the question. (I guess the answer is "yes", but what's the difference between "event loop" and "event queue"?)

  3. None. (Except that Node.js uses both.)

  4. Yes, the event loop is single-threaded.

  5. Yes, browsers have an event loop too (see question 1).




回答2:


This is actually not as simple as given in the post selected as the answer. I hope my remarks will be a bit more exact. Hopefully I understood Sam Roberts (IBM) correctly in his talk on the Node event loop.

To view the talk yourself you can go here: https://www.youtube.com/watch?v=P9csgxBgaZ8

This is an addon to the answer given by @jmrk

Libuv delegates tasks to the underlying operating system. The operating system then becomes responsible for sending a notification when an event occurs you are listening to. It does this for a lot of the operations you perform in Node. Like: sockets (net/dgram/http/tls/https/child_process pipes, stdin, out, err), timeouts and intervals.

However not everything can be delagated like this to the underlying OS. Sometimes it is required to create a thread (there are 4 treads by default but you can change this using UV_THREADPOOL_SIZE). Not pollable are file system operations, dns.lookup() and some crypto functions.




回答3:


Go through these points:-

  1. V8 engine is the engine for coaches of train. It has certain responsibilites including providing event loop to run asynchronous task.

  2. Event loop is the core to perform async tasks. As soon as,C++ Web APIs finish a function(task) , callback is called.It is moved to event queue and waits untill stack becomes empty. Thus, event queue is part of event loop and are generated by event loop.

  3. V8 engine is used to execute the javascript code we write and libuv is a lbrary used to provide multi threading feature in Nodejs to execute long running processes.

  4. Event loop is single threaded but Nodejs is not single threaded as it has a libuv threadpool in its runtime which is responsible for multi threading.

  5. Browsers APIs also provide event loop.



来源:https://stackoverflow.com/questions/49811043/relationship-between-event-loop-libuv-and-v8-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!