【问题标题】:piping to a spawn stdin通过管道连接到 spawn 标准输入
【发布时间】:2014-01-04 02:21:00
【问题描述】:

我想将一些流数据传输到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)

【问题讨论】:

  • 您能否给我们一个示例输入文件/可读流,以便我们可以尝试复制您的问题?另外,您的 gnuplot 和 feedgnuplot 版本是什么?
  • @PaulMougel 我添加了一个可读流的示例。我在 master 运行 gnuplot 4.6 和 feedgnuplot
  • 好吧……您的代码示例对我有用(gnuplot 4.6,来自 master 的 feedgnuplot,节点 v0.10.21,Mac Os 10.9),没有 ECONNRESET 错误。您的操作系统和 Node.js 版本是多少?
  • 嘿,我是个白痴,我没有用我在这里发布的那么少的参数来测试我的代码。我在代码中搞砸了 spawn 的参数。感谢您的帮助。
  • 如果您已经解决了自己的问题,您可以发布自己的答案并接受。这是目前投票率最高的未回答的 gnuplot 问题。

标签: node.js stream streaming pipe gnuplot


【解决方案1】:

对我来说,使用 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)

如果从终端调用nodejs index.js

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 2022-07-05
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    相关资源
    最近更新 更多