gpt4 book ai didi

node.js - 管道到 spawn stdin

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:45 24 4
gpt4 key购买 nike

我想将一些流数据传输到 feedgnuplot为了实时绘制它

以下作品:

// index.js
readableStream.pipe(process.stdout)

// in bash
$ node index.js | feedgnuplot --stream

但以下内容不起作用(这就是我想要做的):

// index.js
var feedgnuplot = require('child_process').spawn('feedgnuplot', ['--stream'])
readableStream.pipe(feedgnuplot.stdin)

//in bash
$ node index.js

我收到一个 ECONNRESET 错误

编辑:请求的可读流示例:

var util = require('util')
var Readable = require('stream').Readable

util.inherits(SomeReadableStream, Readable)

function SomeReadableStream () {
Readable.call(this)
}

SomeReadableStream.prototype._read = function () {
this.push(Math.random().toString()+'\n')
}

var someReadableStream = new SomeReadableStream()

someReadableStream.pipe(process.stdout)

最佳答案

对我来说,使用 node.js v0.10.26,你的脚本运行良好:

脚本index.js:

var util = require('util')
var Readable = require('stream').Readable

util.inherits(SomeReadableStream, Readable)

function SomeReadableStream () {
Readable.call(this)
}

SomeReadableStream.prototype._read = function () {
this.push(Math.random().toString()+'\n')
}

var someReadableStream = new SomeReadableStream()

var feedgnuplot = require('child_process').spawn('feedgnuplot', ['--stream'])
someReadableStream.pipe(feedgnuplot.stdin)

然后从终端调用 if 作为 nodejs index.js

关于node.js - 管道到 spawn stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20615226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com