问题
I am using sax-js to read large xml files. I cannot get the program to exit when the parser is finished. Here is the shape of the script, with parser logic removed.
var fs = require('fs');
var sax = require('sax');
var feedFile = 'foo.xml';
var saxStream = sax.createStream(true)
.on('opentag', function(node) { // do stuff })
.on('end', function() {
console.log("parser end event");
});
var options = {
flags: 'r',
encoding: 'utf8',
mode: 0666,
bufferSize: 1024
};
fs.createReadStream(feedFile, options, function(err) {
throw err;
})
.on('end', function() {
console.log("read stream end event");
})
.pipe(saxStream);
Everything works properly. And the console gets the messages like this
read stream end event
parser end event
And then the process should exit. It does not. What am I missing?
EDIT: What I am missing. There was a resource still open so that node could not exit. The temptation is to start trying to call process.exit() somewhere, anywhere. I tried it myself in this case and it broke stuff.
My script could not exit because I had an open mongodb connection (managed by mongoose). Here is essentially (not in detail) what fixed it:
saxStream.on('end', function() {
console.log("parser end event");
mongoose.disconnect();
};
来源:https://stackoverflow.com/questions/16399476/readstream-pipe-does-not-close