- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试按照 this stackoverflow question 上发布的示例将 node.js 模块作为子进程进行 fork 。 fork 本身可以工作,但我遇到的问题是 Node 试图在 .on('data')
和 .on('exit')
之前添加fork('./DaemonSerial.js'
填充 tChild
var fork = require('child_process').fork;
// Start Serial Daemon/s
var tChild = fork('./DaemonSerial.js', [], {
stdio: 'pipe'
});
tChild.stdin.on('data', function(data) {
// output from the child process
console.log("./DaemonSerial.js >>> " + data)
});
EdgeMaster.Children[tChild.pid] = tChild;
tChild.on('exit', function(d) {
console.log("./DaemonSerial.js >>> "+ tChild.pid + ' Exited:'+ d);
delete EdgeMaster.Children[tChild.pid]
});
我在其他地方也遇到过这个问题,并且我有理由相信应该有一种方法可以强制执行 do THIS then THAT
类型的功能,即使函数本身没有回调。 nodejs.org/api/child_process.html 上的 child_process.fork(modulePath, [args], [options])
没有列出回调。
想法?
编辑: 我编写了一个新脚本 forktest.js
以排除我脚本的其他部分可能导致问题的任何可能性。 forktest.js
具体如下:
var fork = require('child_process').fork;
var ForkDict = {};
function forkit(aPath){
tChild = fork( aPath, [], {stdio: 'pipe'});
ForkDict[tChild.pid] = tChild;
ForkDict[tChild.pid].path = aPath;
tChild.stdout.on('data', function(data) {
// output from the child process
console.log( this.path +'>>> '+ data);
}.bind(this));
tChild.on('exit', function(d) {
console.log( this.path +'>>> Exited:'+ d);
delete ForkDict[tChild.pid]
}.bind(this));
}
forkit('./DaemonSerial.js');
控制台的错误如下:
pi@raspberrypi ~ $ node forktest.js
/home/pi/forktest.js:9
tChild.stdout.on('data', function(data) {
^
TypeError: Cannot call method 'on' of null
at forkit (/home/pi/forktest.js:9:19)
at Object.<anonymous> (/home/pi/forktest.js:19:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
最佳答案
fork
是异步的,但它的返回值不是异步填充的。它返回一个继承自 EventEmitter 的 ChildProcess 实例。 EventEmitter 通常用于异步任务,但您可以在发生某些事情时接收事件,而不是使用回调。就像它在 docs 中所说的那样:
These methods follow the common async programming patterns (accepting a callback or returning an EventEmitter).
在第一个例子中:
fork
没有 stdio
选项。您可能应该改用 silent: true
;它调用 spawn
并将 stdio
选项设置为“pipe”。tChild.stdin.on('data', ...
您可能想使用 stdout
。看起来 stdin
和 stdout
可以是 null
取决于你是否使用 silent: true
.请参阅 fork
文档中的 options.silent
:
Boolean
If true, stdin, stdout, and stderr of the child will be piped to the parent, otherwise they will be inherited from the parent, see the "pipe" and "inherit" options for spawn()'s stdio for more details (default isfalse
)
所以数据只是进入你的主脚本的stdout
。您可以通过以下方式解决此问题(请注意您的 stdio
选项没有执行任何操作):
tChild = fork( aPath, [], {silent: true});
正如我之前所说,您需要在 stdout
上监听 data
事件。
关于javascript - Node.JS fork 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275556/
我正在使用 Assets 管道来管理我的 Grails 3.0 应用程序的前端资源。但是,似乎没有创建 CoffeeScript 文件的源映射。有什么办法可以启用它吗? 我的 build.gradle
我有一个我想要的管道: 提供一些资源, 运行一些测试, 拆资源。 我希望第 3 步中的拆卸任务运行 不管 测试是否通过或失败,在第 2 步。据我所知 runAfter如果前一个任务成功,则只运行一个任
如果我运行以下命令: Measure-Command -Expression {gci -Path C:\ -Recurse -ea SilentlyContinue | where Extensio
我知道管道是一个特殊字符,我需要使用: Scanner input = new Scanner(System.in); String line = input.next
我再次遇到同样的问题,我有我的默认处理方式,但它一直困扰着我。 有没有更好的办法? 所以基本上我有一个运行的管道,在管道内做一些事情,并想从管道内返回一个键/值对。 我希望整个管道返回一个类型为 ps
我有三个环境:dev、hml 和 qa。 在我的管道中,根据分支,阶段有一个条件来检查它是否会运行: - stage: Project_Deploy_DEV condition: eq(varia
我有 Jenkins Jenkins ver. 2.82 正在运行并想在创建新作业时使用 Pipeline 功能。但我没有看到这个列为选项。我只能在自由式项目、maven 项目、外部项目和多配置之间进
在对上一个问题 (haskell-data-hashset-from-unordered-container-performance-for-large-sets) 进行一些观察时,我偶然发现了一个奇
我正在寻找有关如何使用管道将标准输出作为其他命令的参数传递的见解。 例如,考虑这种情况: ls | grep Hello grep 的结构遵循以下模式:grep SearchTerm PathOfFi
有没有办法不因声明性管道步骤而失败,而是显示警告?目前我正在通过添加 || exit 0 来规避它到 sh 命令行的末尾,所以它总是可以正常退出。 当前示例: sh 'vendor/bin/phpcs
我们正在从旧的 Jenkins 设置迁移到所有计划都是声明性 jenkinsfile 管道的新服务器……但是,通过使用管道,我们无法再手动清除工作区。我如何设置 Jenkins 以允许 手动点播清理工
我在 Python 中阅读了有关 Pipelines 和 GridSearchCV 的以下示例: http://www.davidsbatista.net/blog/2017/04/01/docume
我有一个这样的管道脚本: node('linux'){ stage('Setup'){ echo "Build Stage" } stage('Build'){ echo
我正在使用 bitbucket 管道进行培训 这是我的 bitbucket-pipelines.yml: image: php:7.2.9 pipelines: default:
我正在编写一个程序,其中输入文件被拆分为多个文件(Shamir 的 secret 共享方案)。 这是我想象的管道: 来源:使用 Conduit.Binary.sourceFile 从输入中读取 导管:
我创建了一个管道,它有一个应该只在开发分支上执行的阶段。该阶段还需要用户输入。即使我在不同的分支上,为什么它会卡在这些步骤的用户输入上?当我提供输入时,它们会被正确跳过。 stage('Deplo
我正在尝试学习管道功能(%>%)。 当试图从这行代码转换到另一行时,它不起作用。 ---- R代码--原版----- set.seed(1014) replicate(6,sample(1:8))
在 Jenkins Pipeline 中,如何将工件从以前的构建复制到当前构建? 即使之前的构建失败,我也想这样做。 最佳答案 Stuart Rowe 还在 Pipeline Authoring Si
我正在尝试使用 执行已定义的作业构建 使用 Jenkins 管道的方法。 这是一个简单的例子: build('jenkins-test-project-build', param1 : 'some-
当我使用 where 过滤器通过管道命令排除对象时,它没有给我正确的输出。 PS C:\Users\Administrator> $proall = Get-ADComputer -filter *
我是一名优秀的程序员,十分优秀!