Javascript 如何在node.js中超时fs.read?
我尝试使用fs.read从文件描述符读取。调用回调函数 在它得到数据之后。很好用 但是现在我想实现一个超时机制:如果fs.read没有在超时时间内获取数据,它应该停止读取 如何让fs.read停止阅读?它仍然挂在背景上试图阅读 我大致做了以下几点:Javascript 如何在node.js中超时fs.read?,javascript,node.js,timeout,Javascript,Node.js,Timeout,我尝试使用fs.read从文件描述符读取。调用回调函数 在它得到数据之后。很好用 但是现在我想实现一个超时机制:如果fs.read没有在超时时间内获取数据,它应该停止读取 如何让fs.read停止阅读?它仍然挂在背景上试图阅读 我大致做了以下几点: ... var fd = fs.openSync("/dev/ttyUSB0", 'rs+'); ... ... var bout = new Buffer(string,'binary'); fs.write(fd, bout, 0, bout.l
...
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);
});
});
...
...
我想写一些东西到/dev/ttyUSB0并阅读答案,但有时没有答案。如果发生这种情况,读取应该超时,以便我可以开始另一次写入/读取
谢谢
我尝试使用超时和关闭来执行此操作,但不起作用,下面是一个示例:
您必须为测试生成“mkfifo文件”
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");
});
fs.close不起作用。关闭后,您可以创建一个“echo test>file”,然后读取数据。在关闭的文件句柄上读取
有什么想法吗?您不能取消实际的读取操作。唯一的选项是设置计时器并取消订阅(使用removeEventListener)读取操作,然后通过调用带有自定义“timeout”错误的原始事件处理程序继续
希望这对您有所帮助,如果您发布一些代码,我可以向您展示我将如何操作。另一种方法是利用的固有
timeout
参数。其思想是将fs.read
方法放在一个单独的文件中,并作为一个单独的shell进程从主进程执行它。以下是您如何做到这一点:
1-创建一个read.js
脚本,其中包含:
var fs = require('fs');
fs.readFile('/etc/passwd', function (err, data) {
if (err) throw err;
process.send(data);
});
2-在主脚本中,执行read.js
,并等待其消息
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);
});
正如其他人所提到的,您不能取消读取,因此您最好自己创建一个超时,并在读取完成后丢弃结果
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);
}
};
});
对于不提供本机超时功能的函数(如fs.stat、fs.read等),可以使用类似module的内容包装回调 你能行
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'));
添加您的代码。然后我们可以帮助您抱歉,但我不知道如何使用removeEventListener。这是我已经尝试过的。这是可行的,但是“旧”字会发生什么变化呢。它仍然挂在侦听队列中,不是吗?是的,旧的read仍然存在并等待。如果读取超时,可以关闭文件描述符并重新打开它。真的没有办法提前知道你的信是否有答案吗?似乎超时只能解释为需要重新打开文件的错误。关闭文件描述符不起作用,读取仍挂在后台,请参见上面的示例。知道吗?我已经试过了。这是可行的,但是“旧”字会发生什么变化呢。它仍然挂在侦听队列中,不是吗?我想您可以尝试使用这个模块seq Queue,我经常写和读。我没有读完整的文件,但是谢谢你的想法。
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'));