简体   繁体   English

Node.js阻止自然

[英]Node.js Blocking Nature

This is following my earlier question Node.js Non Blocking Nature . 这是我之前的问题Node.js Non Blocking Nature I still can't feel I completely understand what is happening with Node.js . 我仍然感觉不到我完全理解Node.js正在发生的事情。 Lets say Node got a request for a big file upload from a user . 让我们说Node收到了一个用户上传大文件的请求。 Now we say we put the code to handle with the file in a callback , thus making it asynchronous . 现在我们说我们把代码放在回调中处理文件,从而使它异步。 What happens next ? 接下来发生什么 ? Lets say the callback got put in a big list of events the Node.js app needs to take care of , so thus far it is NOT blocking . 让我们说回调被放入Node.js应用程序需要处理的大量事件列表中,所以到目前为止它并没有阻塞。 But at some point the callback gets pulled out from the event loop and it's code starts running (uploading or downloading the file , whatever) . 但是在某些时候,回调会从事件循环中拉出来并且它的代码开始运行(上传或下载文件,无论如何)。 What now ? 现在怎么办 ? Is the Node.js app being blocked now ? Node.js应用程序现在被阻止吗? If so - how does it not destroy the app's performance , and if not - who is in charge of 'jumping' between different events and requests ? 如果是这样 - 它如何不破坏应用程序的性能,如果不是 - 谁负责在不同的事件和请求之间“跳跃”? We understand that in apache or similar servers there is special code to handle all the threads(timing them) so that no thread chokes all the others .In Node however , as I understand , there is an event loop . 我们知道在apache或类似服务器中有特殊代码来处理所有线程(对它们进行计时),这样就没有线程会阻塞所有其他线程。但是,在Node中,我知道,有一个事件循环。 When the file I\\O finishes , it's callback magically returns with the ready file . 当文件I \\ O完成时,它的回调会神奇地返回ready文件。 Nevertheless , the server still has to do the I\\O , no escaping that ! 尽管如此,服务器仍然必须做I \\ O,没有逃避! Is the code in the event loop being timed ? 事件循环中的代码是否正在计时? For example jumping between different events(callbacks..whatever) so that no request chokes the other? 例如,在不同事件之间跳转(callbacks..whatever),以便没有请求扼杀另一个事件? If so , in what manner ? 如果是这样,以什么方式?

All the I/O is handled internally by node.js using multiple threads; 所有I / O都由node.js使用多个线程在内部处理; it's the programming interface to that I/O functionality that's single threaded, event-based, and asynchronous. 它是I / O功能的编程接口,它是单线程,基于事件和异步的。

So the big upload of your example is performed by a separate thread that's managed by node.js, and when that thread completes its work, your callback is put onto the event queue (to be executed by the single JavaScript thread). 因此,您的示例的大上传由一个由node.js管理的单独线程执行,当该线程完成其工作时,您的回调将被放入事件队列(由单个JavaScript线程执行)。

Think of the single JavaScript thread as pulling callbacks off the head of the queue and executing them serially until the queue is empty. 可以将单个JavaScript线程想象为从队列头部拉出回调并连续执行它们直到队列为空。 Then it sleeps until one of the internal I/O threads puts a new callback on the queue. 然后它会一直休眠,直到其中一个内部I / O线程在队列中放入一个新的回调。

This article discusses the related topic of process.nextTick which helped me understand some of these details. 本文讨论了process.nextTick的相关主题,它帮助我理解了其中的一些细节。

Although node.js is often described as "non-blocking", what that really means is that it doesn't block on i/o . 尽管node.js通常被描述为“非阻塞”,但这实际上意味着它不会阻塞i / o If it has to do something processor intensive, sure, other things have to wait. 如果它必须做处理器密集的事情,当然,其他事情必须等待。 But if it is just waiting for data to be returned from the file system or the network, node will not block your process. 但是,如果只是等待从文件系统或网络返回数据,节点将不会阻止您的进程。

In the case of a slow file upload it is processing, periodically it is going to have to do something, as data comes in (possibly calling your callback, but possibly just storing that data until it has enough to call your callback). 在文件上传缓慢的情况下,它正在处理,当数据进入时,它会定期执行某些操作(可能会调用您的回调,但可能只是存储该数据,直到它足以调用您的回调)。 But computers are fast, and whatever happens is, for all intents and purposes, instantaneous. 但计算机速度很快,无论发生什么,无论出于何种意图和目的,都是即时的。 It's the fact that network operations are relatively slow that we are concerned about. 事实上,我们关注的网络运营相对较慢。 In a language like Php, typically, your function will not return until this operation is complete, and there is nothing else you can do in that script, until that happens. 在像Php这样的语言中,通常,在此操作完成之前,您的函数将不会返回,并且在该脚本中您无法执行任何其他操作,直到发生这种情况。 Meanwhile, the CPU could be sitting idle...simply waiting for data to come in across the network. 与此同时,CPU可能处于空闲状态......只需等待数据通过网络进入。 Node, on the other hand, let's you use the CPU for other things, before your callback is called. 另一方面,节点让你在调用回调之前将CPU用于其他事情。

As others have noted, there may be threads happening under the hood. 正如其他人所指出的那样,可能会出现一些线索。 Or there may not be, since node could internally implement file and network operations using non-blocking C++ functions. 或者可能没有,因为节点可以使用非阻塞C ++函数在内部实现文件和网络操作。 Doesn't matter. 无所谓。 The important thing is that i/o operations do not block other things from happening within your app. 重要的是,i / o操作不会阻止您的应用程序中发生其他事情。

I think this subject is complicated enough (at least for newcomers) to deserve a thorough read and can't be understood by read a 5 line answer. 我认为这个主题足够复杂(至少对于新手来说)值得彻底阅读,并且通过阅读5行答案无法理解。 This is the best resource I found http://www.theserverside.com/discussions/thread.tss?thread_id=61693 . 这是我找到的最好的资源http://www.theserverside.com/discussions/thread.tss?thread_id=61693 He doesn't think so highly of Node.js but he does take a lot of effort to explain it . 他对Node.js并不那么认真,但他确实需要付出很多努力来解释它。 Worth a read in my opinion. 值得一读在我看来。

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

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