最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to timeout an fs.read in node.js? - Stack Overflow

programmeradmin0浏览0评论

I try to read from a filedescriptor with fs.read. The callback-function is called after it got the data. It's working fine.

But now I want to implement a timeout mechanism: If the fs.read does not get data within the timeout it should stop reading.

How can I tell fs.read to stop reading? It is still hanging in the background trying to read.

I do roughly the following:

...
var fd = fs.openSync("/dev/ttyUSB0", 'rs+');
...
...
var bout = new Buffer(string,'binary');
fs.write(fd, bout, 0, bout.length,undefined,function(err, written, buffer) {
  console.log("error writing: "+err);

  var bin = new Buffer(1);
  fs.read(fd,bin,0,1,undefined,function(err,read,buffer) {
    console.log("got it: "+bin);
  });
});
...
...

I want to write something to /dev/ttyUSB0 and read an answer, but sometimes there is no answer. If this happens, the read should timeout so that I can start another write/read.

Thanks

I tried to do it with Timeout and close, but it is not working, here is an example: You have to make "mkfifo file" for the test.

var fs=require('fs');

console.log("open file");

fs.open('file', 'r+', function(err,fd) {

  console.log("start reading on "+fd);

  var length=5;

  var data = new Buffer(length);
  data.fill("-");

  setTimeout(function(){
    console.log("timeout");
    console.log("close fd "+fd);
    fs.close(fd,function(err) {
      console.log("close done: "+err);
    });
  }, 5000);

  fs.read(fd, data, 0, length,undefined, function(error, got) {
    console.log("error: "+error);
    console.log("got callback: "+data);
  });

  console.log("done");
});

The fs.close does not work. After it has closed you can make a "echo test > file" and then the read get the data. Reading on a closed Filehandle?

Any Idea?

I try to read from a filedescriptor with fs.read. The callback-function is called after it got the data. It's working fine.

But now I want to implement a timeout mechanism: If the fs.read does not get data within the timeout it should stop reading.

How can I tell fs.read to stop reading? It is still hanging in the background trying to read.

I do roughly the following:

...
var fd = fs.openSync("/dev/ttyUSB0", 'rs+');
...
...
var bout = new Buffer(string,'binary');
fs.write(fd, bout, 0, bout.length,undefined,function(err, written, buffer) {
  console.log("error writing: "+err);

  var bin = new Buffer(1);
  fs.read(fd,bin,0,1,undefined,function(err,read,buffer) {
    console.log("got it: "+bin);
  });
});
...
...

I want to write something to /dev/ttyUSB0 and read an answer, but sometimes there is no answer. If this happens, the read should timeout so that I can start another write/read.

Thanks

I tried to do it with Timeout and close, but it is not working, here is an example: You have to make "mkfifo file" for the test.

var fs=require('fs');

console.log("open file");

fs.open('file', 'r+', function(err,fd) {

  console.log("start reading on "+fd);

  var length=5;

  var data = new Buffer(length);
  data.fill("-");

  setTimeout(function(){
    console.log("timeout");
    console.log("close fd "+fd);
    fs.close(fd,function(err) {
      console.log("close done: "+err);
    });
  }, 5000);

  fs.read(fd, data, 0, length,undefined, function(error, got) {
    console.log("error: "+error);
    console.log("got callback: "+data);
  });

  console.log("done");
});

The fs.close does not work. After it has closed you can make a "echo test > file" and then the read get the data. Reading on a closed Filehandle?

Any Idea?

Share Improve this question edited Dec 30, 2013 at 11:55 Oliver Joa asked Dec 27, 2013 at 21:33 Oliver JoaOliver Joa 512 silver badges4 bronze badges 2
  • add your code.then we can help you – jmingov Commented Dec 27, 2013 at 21:49
  • could be easy with os.dup but that is not implemented in node – milahu Commented Jul 3, 2022 at 17:05
Add a ment  | 

6 Answers 6

Reset to default 2

One other way would be to take advantage of child_process.exec's inherent timeout parameter. The idea is to put the fs.read method in a separate file, and execute it from the main process as a separate shell process. Here is how you could do it:

1- Create a read.js script, which contains:

var fs = require('fs');
fs.readFile('/etc/passwd', function (err, data) {
  if (err) throw err;
  process.send(data);
});

2- In your main script, execute read.js, and wait for its message.

var exec = require('child_process').exec,
    child;

child = exec('node read.js',
  { timeout : 5000 }, //5 sec timeout
  function (error, stdout, stderr) {...}
});

child.on('message', function(data) {
  console.log(data);
});

You cannot cancel the actual read operation. The only option is to set a timer and unsubscribe (using removeEventListener) from the read operation and continue by calling the original event handler with a custom "timeout" error.

Hope this helps and if you post some code I can show you how I would do it.

var fs = require('fs');
var status = 0; // 0:start 1:fin
var timeout = 500;

fs.readFile('filename', function(err,data){
   status = 1
   console.log(data)
});

var timer = setTimeout(function(){
  clearTimeout(timer);
  if(!status){

    throw new Error('timeout');
  }
}, timeout);

As others have mentioned, you cannot cancel the read, so the best you can do is create a timeout yourself and discard the result when it's finishes.

fs.read(..., timeout(1000, function(err, result){

}));

function timeout(time, cb){
  var called = false;
  var timer = setTimeout(function(){
    // If the timer finishes before the function is called,
    // then run the callback with an error. 
    if (!called){
       called = true;
       cb(new Error("Function timed out."));
    }
  }, time);

  return function(){
    // If the read finishes before the timer, cancel the timer
    // and call the original callback.
    if (!called){
      clearTimeout(timer);
      called = true;
      cb.apply(this, arguments);
    }
  };
});

For functions that do not provide a native timeout capability (like fs.stat, fs.read, etc..) you can wrap the callback using something like callback-timeout module.

You can do

const timeout = require('callback-timeout');
const fs = require('fs');

fs.stat('/mnt/never-returns', timeout((err, stats)=>{
   if(err) throw err;
   //happy with stats
}, 2000, 'fs.stat did not return in 2 secs'));

fs.readSync with timeout, using child_process.spawnSync to call dd

calling dd (max RSS 4 MB) is cheaper than calling node (max RSS 40 MB)

unix only. on windows this may work with busybox dd

#!/usr/bin/env node

// readWithTimeout.js

const child_process = require('child_process');
const fs = require('fs');

/**
* read with timeout. unix only
* @param {number | string} fdOrPath
* @param {number} blockSize
* @param {number} timeout
* @param {Object} options
* @param {number} [options.numBlocks=1]
* @param {string=} options.encoding
*/
function readWithTimeout(fdOrPath, blockSize, timeout, options = {}) {
  if (!options) options = {};
  const numBlocks = options.numBlocks || 1;
  if (options.numBlocks) delete options.numBlocks;
  if (options.timeout) throw Error('dont set options.timeout');
  const ddArgs = [`bs=${blockSize}`, `count=${numBlocks}`, 'status=none'];
  const stdio = [fdOrPath, 'pipe', 'pipe'];
  if (typeof fdOrPath == 'string') {
    if (!fs.existsSync(fdOrPath)) throw Error(`no such file: ${fdOrPath}`);
    ddArgs.push(`if=${fdOrPath}`);
    stdio[0] = null;
  }
  else if (typeof fdOrPath != 'number') {
    throw Error(`fdOrPath must be number or string`);
  }
  //console.dir({ fdOrPath, blockSize, timeout, stdio, ddArgs });
  const reader = child_process.spawnSync('dd', ddArgs, {
    timeout,
    stdio,
    windowsHide: true,
    ...options,
  });
  if (reader.error) throw reader.error;
  return reader.stdout;
}

// demo: read 1 byte from fd 0 = stdin
// ./readWithTimeout.js # -> timeout
// echo 12 | ./readWithTimeout.js # -> '1'
try {
  const readFd = 0; // stdin
  const readLen = 1;
  const readTimeout = 1000; // milliseconds
  const output = readWithTimeout(readFd, readLen, readTimeout, {
  //const output = readWithTimeout('/dev/null', readLen, readTimeout, {
    encoding: 'utf8',
  });
  if (output.length == 0) {
    console.log(`read nothing`);
  }
  else if (output.length < readLen) {
    console.log(`read partial: ${output.length} of ${readLen} bytes`);
  }
  else {
    console.log('read ok');
  }
  console.dir({ output });
}
catch (e) {
  if (e.errno == -110) {
    console.log('read error: timeout');
  }
  else {
    console.log('read error:');
    console.dir(e);
  }
}
$ ./readWithTimeout.js
read error: timeout

$ echo 12 | ./readWithTimeout.js
read ok
{ output: '1' }

this is part of my gnumake-tokenpool for javascript

code also in read-with-timeout

based on the answer by verybadalloc

发布评论

评论列表(0)

  1. 暂无评论