【问题标题】:how to use child_process.fork in meteor如何在流星中使用 child_process.fork
【发布时间】:2014-01-11 07:49:46
【问题描述】:

我在meteor中测试代码。有一些错误。

/server/lib/Test.js

测试 = { 说:函数(){ console.log("你好世界"); } }

/server/main.js

Meteor.startup(函数(){ 测试.say(); //运行良好 var path = Npm.require('path'); var base = path.resolve('.'); var n = Npm.require('child_process').fork(base+"/app/server/children.js"); n.on('消息', function(m) { console.log('家长收到消息:', m); }); n.send({你好:'世界'}); });

/server/children.js

process.on('消息', function(m) { 测试.say(); //抛出一个错误。测试未定义 console.log('孩子收到消息:', m); process.send({name:"ABC"}); }); process.send({ foo: 'bar' });

错误:

I20131223-16:34:44.717(8)?你好世界 W20131223-16:34:44.784(8)? (STDERR)/home/ec/workspace/meteor/testChildrenProcess/.meteor/local/build/programs/server/app/server/children.js:2 W20131223-16:34:44.784(8)? (STDERR) Test.say(); W20131223-16:34:44.785(8)? (标准错误)^ W20131223-16:34:44.785(8)? (STDERR)ReferenceError:未定义测试 W20131223-16:34:44.787(8)? (STDERR) 在处理中。 (/home/ec/workspace/meteor/testChildrenProcess/.meteor/local/build/programs/server/app/server/children.js:2:3) W20131223-16:34:44.788(8)? (STDERR) 在 process.EventEmitter.emit (events.js:98:17) W20131223-16:34:44.788(8)? (STDERR) 在 handleMessage (child_process.js:318:10) W20131223-16:34:44.788(8)? (STDERR) 在 Pipe.channel.onread (child_process.js:345:11)

测试对象在“/app/server/children.js”中未定义,但在 main.js 中是正常的。 为什么?我忘了什么吗?任何帮助表示赞赏。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    变量是作用域的。虽然在 Meteor 中 Test 可用于您的流星项目中的其他文件,但它们不适用于 Npm.require 所需的 Npm 模块/东西。

    children.js 与您的 main.js 文件位于不同的命名空间中。要访问其中的 Test 变量,您需要将参数或作为消息传递给它。

    【讨论】:

    • 感谢您的意见。但是当我在Meteor.startupn.send({ hello: 'world',Test:Test });。在 children.js 中无法获取函数 say
    • 不太确定可以传递的数据类型,可以传递参数但不确定整个对象,因为它是一个单独的过程。见stackoverflow.com/questions/15368436/…
    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多