简体   繁体   English

事件驱动的编程node.js?

[英]Event driven programming node.js?

I was going through this article, i have one particular doubt if some one clear out to me. 我正在阅读本文,我特别怀疑是否有人对我清楚。

http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb

var fs = require('fs')
  , sys = require('sys');

fs.readFile('treasure-chamber-report.txt', function(report) {
  sys.puts("oh, look at all my money: "+report);
});

fs.writeFile('letter-to-princess.txt', '...', function() {
  sys.puts("can't wait to hear back from her!");
});

Your code gives node the two tasks to read and write a file, and then goes to sleep. 您的代码为节点提供了读写文件的两项任务,然后进入睡眠状态。 Once node has completed a task, the callback for it is fired. 节点完成任务后,将触发其回调。 But there can only be one callback firing at the same time. 但是一次只能触发一个回调。 Until that callback has finished executing, all other callbacks have to wait in line. 在该回调完成执行之前,所有其他回调都必须排队等待。 In addition to that, there is no guarantee on the order in which the callbacks will fire. 除此之外,不能保证回调的触发顺序。

"So I don't have to worry about code accessing the same data structures at the same time?" “因此,我不必担心代码同时访问相同的数据结构吗?” You got it! 你说对了! That's the entire beauty of JavaScripts single-threaded / event loop design! 这就是JavaScript单线程/事件循环设计的全部美!

  1. Can anyone explain me the above bold lines.? 谁能向我解释以上粗体字? How come we are not able to worry that the two different programs would not access the object. 为什么我们不能担心两个不同的程序将无法访问该对象。
  2. How come the current way of threading has problem? 当前的线程方式怎么会出现问题?
  3. Will the order of firing the callback's be a problem? 触发回调的顺序会不会有问题? Let's take i want callBack A() to return first before callBack b(). 让我们让我希望callBack A()在callBack b()之前先返回。

1) If you're running single threaded, you don't have to worry about the problems that come with a multi-threaded application. 1)如果您正在运行单线程,则不必担心多线程应用程序附带的问题。 This includes 2 different threads trying to use the same object at the same time. 这包括两个试图同时使用同一对象的不同线程。 Imagine, for example, if one thread was trying to read data from a hash, while another thread was deleting data from the same hash. 例如,想象一下,如果一个线程试图从哈希中读取数据,而另一个线程正在从同一哈希中删除数据。 A key/value pair may look like it was present in one line of code, but because of threading by the time it reaches the next line the data may not be there anymore. 键/值对可能看起来像是存在于一行代码中,但是由于线程在到达下一行时已经不存在,因此数据可能不再存在。 Similarly, you don't have to deal with all the extra code and headaches involved in avoiding these problems. 同样,您不必处理避免这些问题所涉及的所有额外代码和麻烦。

2) See #1. 2)参见#1。 It's not a problem so much as a tradeoff. 这不是一个权衡的问题。 Lots of computers these days have multiple processors/cores, so having a program use more than one thread at a time can be beneficial. 如今,许多计算机都具有多个处理器/内核,因此让程序一次使用多个线程可能是有益的。 It is also useful when you expect a thread to be blocking. 当您期望线程被阻塞时,它也很有用。 For example, in another multi-threaded language it would be common to read the contents of the file, and the output them without the addition of a callback. 例如,在另一种多线程语言中,通常会读取文件的内容,然后在不添加回调的情况下将其输出。 However, this means that thread sits there doing nothing (blocked) until the file read operation is done. 但是,这意味着在文件读取操作完成之前,线程坐在那里什么都不做(被阻塞)。 Multi-threaded programming is also very hard to do correctly. 多线程编程也很难正确完成。

3) You wouldn't not be gauranteed order. 3)您不会受到保证。 If you want to ensure proper order, you wait to execute the 2nd call until the first has returned. 如果要确保顺序正确,请等待执行第二个调用,直到第一个调用返回。 eg 例如

fs.readFile('treasure-chamber-report.txt', function(report) {
    sys.puts("oh, look at all my money: "+report);

    fs.writeFile('letter-to-princess.txt', '...', function() {
      sys.puts("can't wait to hear back from her!");
    });
});

Note that this can sometimes get you into what is commonly referred to as 'callback hell' 请注意,有时这可以使您进入通常称为“回调地狱”的地方

EDIT: To address your comments: 编辑:解决您的评论:

1) Even though you are "waiting" for NodeJS for the file to be read, in NodeJS this is a non-blocking operation. 1)即使您正在“等待” NodeJS读取文件,在NodeJS中这也是一个非阻塞操作。 That means the method call ( readFile ) returns immediately, even before the file is read. 这意味着方法调用( readFile )立即返回,甚至在读取文件之前也是如此。 This is because it passes off the data IO request to the underlying operating system, which has it's own threads for processing such requests. 这是因为它将数据IO请求传递给基础操作系统,该操作系统具有自己的线程来处理此类请求。 When the operating system finishes the read (or write), it notifies the originating process that it is ready with the data. 当操作系统完成读取(或写入)时,它将通知原始进程已准备好处理数据。 While the operating system is doing this work, NodeJS can let it's one thread continue on doing other work while it waits. 在操作系统执行此工作的同时,NodeJS可以让它的一个线程在等待时继续执行其他工作。 That's why you need the callback - you need a way to tell NodeJS what to do next when you finally get the data. 这就是为什么需要回调的原因-当最终获得数据时,需要一种方法来告诉NodeJS接下来该做什么。

2) There is nothing inherently bad about callback hell, other than it is hard to read. 2)除了很难阅读之外,回调地狱本质上没有什么坏处。 One might imagine that if what you are trying to do includes several different asynchronous processes (like reading and writing to disk), that you might get something that looks like this: 可能会想到,如果您尝试执行的操作包含多个不同的异步进程(例如,对磁盘进行读写),那么您可能会得到如下所示的内容:

var doSomething function(){
    fs.readFile('step1.txt', function(result){
        // do something with the result
        fs.writeFile('step2.txt', function(){
            // okay, step2 is ready, so process that
            fs.readFile('step2.txt', function(result){
                fs.writeFile('step3.txt', function(){
                    //etc, etc
                });
            });
        });
    });
}

You can see that nesting can get rather deep rather quickly, and get difficult to read. 您会看到嵌套会很快变得很深,并且很难阅读。 If you search for "JavaScript callback hell" there are many discussions here and elsewhere that talk about this. 如果您搜索“ JavaScript回调地狱”,那么这里和其他地方都会有很多讨论。 One approach to flatten things out is to avoid inline/anonymous functions, and flatten it out with named functions, so you callbacks are not nested so deep in your editor (though the nesting is still happening from a lexical standpoint). 整理内容的一种方法是避免使用内联/匿名函数,并使用命名函数对其进行整理,这样您的回调函数就不会嵌套在编辑器中那么深(尽管从词法的角度来看嵌套仍在进行)。

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

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