node.js进程如何知道何时停止? [英] How does a node.js process know when to stop?

查看:150
本文介绍了node.js进程如何知道何时停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于许多node.js脚本遵循异步执行模式(以下示例),因此它们如何知道何时停止?

Since many node.js scripts follow a pattern of doing something asynchronously (example below), how do they know when to stop?

在下面的代码中,节点如何确定在处理writeFile并适当注册回调之后,在回调运行之前,该过程应保持活动状态?

In the following code, how does node determine after processing the writeFile, and registering the callback appropriately, that the process should be kept alive until the callback(s) run?

fs = require('fs');

fs.writeFile('foo', 'cat', function() {
  console.log('wrote to foo!'); 
  fs.readFile('foo', 'utf8', function(err, data) {
    console.log(data);
  });
}); 

推荐答案

节点会跟踪所有未完成的工作请求.您的fs.writefile()调用为I/O创建工作请求,并将您的回调添加到该请求.节点在启动I/O活动的同时将工作请求保存到其表中.当您到达函数末尾时,代码执行将退出. (但是您的内存/变量/等仍然存在)

node keeps track of all outstanding work requests. Your fs.writefile() call creates a work request for I/O and adds your callback to that request. node saves the work request into its tables at the same time it is starting the I/O activity. Your code execution exits when you reach the end of your function. (But your memory/variables/etc. remain)

稍后,I/O完成,并且节点将工作请求从其表中取出.它会看到附加到请求的回调,因此会使用I/O请求的结果来调用该函数.您的全局数据仍然存在,并且闭包中的任何变量仍然存在,因此在您的代码中似乎从未停止过.

Later the I/O finishes and node takes the work request out of its tables. It sees the callback attached to the request and so calls that function with the results of the I/O request. Your global data is still around and any variables in closures still exist, so it seems to your code like it never stopped.

如果您什么也不做,不要再发出任何请求,那么当您从函数节点返回时,该节点将停止,因为这样队列中就不会再有任何剩余的请求了.

If you do nothing more, don't make any more requests, then when you return from your functions node will stop, because then there won't be any remaining requests in the queues.

所以节点'知道'继续运行,因为它跟踪其表中的活动工作请求,并且直到所有排队的工作都完成并且这些表为空时才会停止.

So node 'knows' to keep running because it tracks active work requests in its tables and won't stop until all queued work is done and those tables are empty.

请注意,排队工作"可能包括诸如等待计时器或等待网络数据到达之类的事情.您发出一个请求,内容为稍后/如果稍后发生情况,请致电给我".

Please note that "queued work" can include things like waiting for timers or waiting for network data to arrive. You make a request that says "call me here when/if something happens later".

setTimeout()也是一个工作请求(如果您斜视一下).使用计时器,您知道会发生什么以及何时发生.使用setTimeout()只会发生一个事情".节点将仅对您的回调进行一次调用,然后忘记"工作请求.相反,如果您使用setInterval(),则创建了一个持久性工作请求.节点将保留"工作请求在其表中,并将反复调用回调,直到您取消该请求为止.

setTimeout() is a work request too (if you squint a bit). With a timer you know something will happen and when it will happen. With setTimeout() only one 'something' will happen. node will make only one call to your callback and then 'forget' the work request. If instead you use setInterval() you have created a persistent work request. node will 'keep' the work request in its tables and will call your callback repeatedly, until you cancel the request.

net.Server.listen()是另一个工作请求,它是一个持久性工作请求.您不知道何时调用回调或调用多少次,因为这取决于连接到服务器的远程客户端.节点将工作请求保持在其表中,直到您取消该请求为止.

net.Server.listen() is another work request which is a persistent work request. You don't know when your callback will be called or how many times, because that depends on remote clients connecting to your server. node keeps the work request alive in its tables until you cancel the request.

这篇关于node.js进程如何知道何时停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆