用node.js编写到文件系统的非阻塞方式。

时间:2021-11-25 21:46:23

I've written a non-blocking tcp-server with node.js. This server listens on a port and reroutes the request to an other server via a http.request()

我已经用node.js编写了一个非阻塞的tcp-server。此服务器监听端口并通过http.request()将请求重新路由到另一个服务器

To have a back-log of the messages rerouted I want to append every message (single line of information) in a file with the date as filename.

要有重新路由的消息的后日志,我希望将每个消息(单行信息)附加到一个文件中,并将日期作为文件名。

The server is going to be hit by several devices on alternating intervals with small txt strings (800bytes). Writing to the filesystem implicitly calls for a blocking event. Is there a way to prevent this behavior??

服务器将被几个设备以不同的间隔和小的txt字符串(800字节)访问。向文件系统写入隐式地调用一个阻塞事件。有没有防止这种行为的方法?

3 个解决方案

#1


3  

If appendFile doesn't work out right, I have myself tested a solution for this using File streams that works with multiple clusters and won't clobber the output

如果appendFile不能正常工作,我已经使用文件流测试了解决方案,该文件流可以处理多个集群,不会影响输出

#2


1  

Just use the asynchronous methods of the fs module like appendFile.

只需使用fs模块的异步方法,如appendFile。

http://nodejs.org/api/fs.html#fs_fs_appendfile_filename_data_encoding_utf8_callback

http://nodejs.org/api/fs.html fs_fs_appendfile_filename_data_encoding_utf8_callback

#3


0  

Something like this might help.

像这样的东西可能会有帮助。

    var fs = require('fs');
    var writer = {
      files: {},
      appendFile: function(path, data) {
        if(this.files[path] === undefined) {
          this.files[path] = {open: false, queue: []};
        }
        this.files[path].queue.push(data);
        if(!this.files[path].open) {
          this.files[path].open = true;
          this.nextWrite(path);
        }
      },
      nextWrite: function(path) {
        var data = this.files[path].queue.shift(),
            self = this;
        if(data === undefined)
          return this.files[path].open = false;
        fs.appendFile(path, data, function(err) {
          if (err) throw err;
          self.nextWrite(path);
        });
      }
    }

It requires version 0.8.0 of node for fs.appendFile, but it keeps a queue per file and then appends the things in the order they were added. It works, but I didn't spent very much time on it.. so use it for educational purposes only.

它需要fs的0.8.0版本。appendFile,但是它为每个文件保留一个队列,然后按照添加的顺序添加内容。这方法很有效,但我没有花太多时间。所以只用于教育目的。

   writer.appendFile('test.txt','hello');

#1


3  

If appendFile doesn't work out right, I have myself tested a solution for this using File streams that works with multiple clusters and won't clobber the output

如果appendFile不能正常工作,我已经使用文件流测试了解决方案,该文件流可以处理多个集群,不会影响输出

#2


1  

Just use the asynchronous methods of the fs module like appendFile.

只需使用fs模块的异步方法,如appendFile。

http://nodejs.org/api/fs.html#fs_fs_appendfile_filename_data_encoding_utf8_callback

http://nodejs.org/api/fs.html fs_fs_appendfile_filename_data_encoding_utf8_callback

#3


0  

Something like this might help.

像这样的东西可能会有帮助。

    var fs = require('fs');
    var writer = {
      files: {},
      appendFile: function(path, data) {
        if(this.files[path] === undefined) {
          this.files[path] = {open: false, queue: []};
        }
        this.files[path].queue.push(data);
        if(!this.files[path].open) {
          this.files[path].open = true;
          this.nextWrite(path);
        }
      },
      nextWrite: function(path) {
        var data = this.files[path].queue.shift(),
            self = this;
        if(data === undefined)
          return this.files[path].open = false;
        fs.appendFile(path, data, function(err) {
          if (err) throw err;
          self.nextWrite(path);
        });
      }
    }

It requires version 0.8.0 of node for fs.appendFile, but it keeps a queue per file and then appends the things in the order they were added. It works, but I didn't spent very much time on it.. so use it for educational purposes only.

它需要fs的0.8.0版本。appendFile,但是它为每个文件保留一个队列,然后按照添加的顺序添加内容。这方法很有效,但我没有花太多时间。所以只用于教育目的。

   writer.appendFile('test.txt','hello');