Ho cercato di utilizzare uno stream leggibile e uno di trasformazione per elaborare un file molto grande. Il problema che mi sembra di incontrare è che se non metto un flusso scrivibile alla fine, il programma sembra terminare prima che il risultato venga restituito.Node.js Stream leggibili per trasformare
Esempio: rstream.pipe(split()).pipe(tstream)
mio tstream
ha un emettitore che emette quando un contatore colpisce una soglia. Quando quella soglia è impostata su un numero basso, ottengo un risultato, ma quando è alto, non restituisce nulla. Se lo instrado a un autore di file, restituisce sempre un risultato. Mi manca qualcosa di ovvio?
codice:
// Dependencies
var fs = require('fs');
var rstream = fs.createReadStream('file');
var wstream = fs.createWriteStream('output');
var split = require('split'); // used for separating stream by new line
var QTransformStream = require('./transform');
var qtransformstream = new QTransformStream();
qtransformstream.on('completed', function(result) {
console.log('Result: ' + result);
});
exports.getQ = function getQ(filename, callback) {
// THIS WORKS if i have a low counter for qtransformstream,
// but when it's high, I do not get a result
// rstream.pipe(split()).pipe(qtransformstream);
// this always works
rstream.pipe(split()).pipe(qtransformstream).pipe(wstream);
};
Ecco il codice per il Qtransformstream
// Dependencies
var Transform = require('stream').Transform,
util = require('util');
// Constructor, takes in the Quser as an input
var TransformStream = function(Quser) {
// Create this as a Transform Stream
Transform.call(this, {
objectMode: true
});
// Default the Qbase to 32 as an assumption
this.Qbase = 32;
if (Quser) {
this.Quser = Quser;
} else {
this.Quser = 20;
}
this.Qpass = this.Quser + this.Qbase;
this.Counter = 0;
// Variables used as intermediates
this.Qmin = 120;
this.Qmax = 0;
};
// Extend the transform object
util.inherits(TransformStream, Transform);
// The Transformation to get the Qbase and Qpass
TransformStream.prototype._transform = function(chunk, encoding, callback) {
var Qmin = this.Qmin;
var Qmax = this.Qmax;
var Qbase = this.Qbase;
var Quser = this.Quser;
this.Counter++;
// Stop the stream after 100 reads and emit the data
if (this.Counter === 100) {
this.emit('completed', this.Qbase, this.Quser);
}
// do some calcs on this.Qbase
this.push('something not important');
callback();
};
// export the object
module.exports = TransformStream;
Puoi pubblicare il codice per l'implementazione 'QTransformStream'? – mscdex
Quante linee hai nel file di input e qual è il valore massimo del contatore in quel caso. Se il valore del contatore è maggiore dei numeri di riga, l'evento 'completato' non emetterà. Inoltre è necessario premere 'null' per terminare il flusso. Non sei sicuro di cosa hai in "qualcosa di non importante", ma a un certo punto dovrebbe esserci un "nulla". – hassansin
Ci sono meno linee rispetto al contatore, circa 7000 linee. Funziona quando lo canalizzo in un flusso di scrittura. Un flusso di trasformazione deve avere una spinta (null) per funzionare? – ace040686