Node.js异步文件I / O.

时间:2021-09-02 20:53:53

I'm new to Node.js and recently learned about the fs module. I'm a little confused about asynchronous vs. synchronous file i/o.

我是Node.js的新手,最近了解了fs模块。我对异步与同步文件i / o有点困惑。

Consider the following test:

考虑以下测试:

var fs = require('fs');

var txtfile = 'async.txt';
var buffer1 = Buffer(1024);
var buffer2 = '1234567890';

fs.appendFile(txtfile, buffer1, function(err) {
    if (err) { throw err };
    console.log('appended buffer1');
});

fs.appendFile(txtfile, buffer2, function(err) {
    if (err) { throw err };
    console.log('appended buffer2');
});

About half the time when I run this, it prints appended buffer2 before appended buffer1. But when I open the text file, the data always appears to be in the right order - a bunch of garbage from Buffer(1024) followed by 1234567890. I would have expected the reverse or a jumbled mess.

大约一半的时间我运行它,它会在附加的buffer1之前打印附加的buffer2。但是当我打开文本文件时,数据总是看起来是正确的顺序 - 来自Buffer(1024)的一堆垃圾后跟1234567890.我本来期望反向或乱七八糟的混乱。

What's going on here? Am I doing something wrong? Is there some kind of lower-level i/o queue that maintains order?

这里发生了什么?难道我做错了什么?是否有某种维持秩序的低级i / o队列?

I've seen some talk about filesystem i/o differences with Node; I'm on a Mac if that makes any difference.

我已经看到一些关于文件系统与Node的i / o差异的讨论;我在Mac上,如果这有任何区别。

1 个解决方案

#1


5  

From my understanding, although the code is asynchronous, at the OS level, the file I/O operations of the SAME file are not. That means only one file I/O operation is processing at a time to a single file.

根据我的理解,尽管代码是异步的,但在操作系统级别,SAME文件的文件I / O操作却不是。这意味着一次只有一个文件I / O操作处理单个文件。

During the 1st append is occurring, the file is locked. Although the 2nd append has been processed, the file I/O part of it is put in the queue by the OS and finishes with no error status. My guess is the OS does some checks to make sure the write operation will be successful such as file exists, is writable, and diskspace is large enough, and etc. If all those conditions met, the OS returns to the application with no error status and will finish the writing operation later when possible. Since the buffer of the 2nd append is much smaller, it might finish processing (not writing to file part of it) before first append finished writing to file. You, therefore, saw the 2nd console.log() first.

在第一次附加发生期间,文件被锁定。虽然第二个附加已被处理,但它的文件I / O部分由OS放入队列并完成,没有错误状态。我的猜测是操作系统会做一些检查以确保写操作成功,例如文件存在,可写,磁盘空间足够大等等。如果满足所有这些条件,操作系统将返回应用程序,没有错误状态并在可能的情况下稍后完成写作操作。由于第二个附加的缓冲区要小得多,因此在第一次附加完成写入文件之前可能会完成处理(不写入文件的一部分)。因此,您首先看到了第二个console.log()。

#1


5  

From my understanding, although the code is asynchronous, at the OS level, the file I/O operations of the SAME file are not. That means only one file I/O operation is processing at a time to a single file.

根据我的理解,尽管代码是异步的,但在操作系统级别,SAME文件的文件I / O操作却不是。这意味着一次只有一个文件I / O操作处理单个文件。

During the 1st append is occurring, the file is locked. Although the 2nd append has been processed, the file I/O part of it is put in the queue by the OS and finishes with no error status. My guess is the OS does some checks to make sure the write operation will be successful such as file exists, is writable, and diskspace is large enough, and etc. If all those conditions met, the OS returns to the application with no error status and will finish the writing operation later when possible. Since the buffer of the 2nd append is much smaller, it might finish processing (not writing to file part of it) before first append finished writing to file. You, therefore, saw the 2nd console.log() first.

在第一次附加发生期间,文件被锁定。虽然第二个附加已被处理,但它的文件I / O部分由OS放入队列并完成,没有错误状态。我的猜测是操作系统会做一些检查以确保写操作成功,例如文件存在,可写,磁盘空间足够大等等。如果满足所有这些条件,操作系统将返回应用程序,没有错误状态并在可能的情况下稍后完成写作操作。由于第二个附加的缓冲区要小得多,因此在第一次附加完成写入文件之前可能会完成处理(不写入文件的一部分)。因此,您首先看到了第二个console.log()。