Node.js 使用child_process.send in CoffeeScript时缺少消息

Node.js 使用child_process.send in CoffeeScript时缺少消息,node.js,process,coffeescript,Node.js,Process,Coffeescript,我刚开始使用咖啡脚本,我喜欢它,但我遇到了一个令人沮丧的问题。当我在javascript中重现进程和分支子进程之间发送和接收消息的基本版本时,我得到了预期的结果。到目前为止,一切都很好 -----------app2.js----------------------------- var child = require('child_process'); var forked = child.fork("./child2.js"); forked.on('message', function

我刚开始使用咖啡脚本,我喜欢它,但我遇到了一个令人沮丧的问题。当我在javascript中重现进程和分支子进程之间发送和接收消息的基本版本时,我得到了预期的结果。到目前为止,一切都很好

-----------app2.js-----------------------------

var child = require('child_process');
var forked = child.fork("./child2.js");

forked.on('message', function (msg) {
    console.log("parent recieved ", msg);
});

forked.send({hello:'world'});
process.on('message', function(m) {
    console.log("child received ", m);
});

process.send({foo:'bar'});
cp = require('child_process')
n = cp.fork("./child.coffee")

n.on 'message', (m) =>
    console.log 'PARENT recieved', m

n.send {foo:'hello from the parent process'}
process.on 'message', (m) =>
    console.log 'CHILD received ', m

console.log "Child process running"

process.send {bar:'hello from the child process'}
----------child2.js--------------------------------

var child = require('child_process');
var forked = child.fork("./child2.js");

forked.on('message', function (msg) {
    console.log("parent recieved ", msg);
});

forked.send({hello:'world'});
process.on('message', function(m) {
    console.log("child received ", m);
});

process.send({foo:'bar'});
cp = require('child_process')
n = cp.fork("./child.coffee")

n.on 'message', (m) =>
    console.log 'PARENT recieved', m

n.send {foo:'hello from the parent process'}
process.on 'message', (m) =>
    console.log 'CHILD received ', m

console.log "Child process running"

process.send {bar:'hello from the child process'}
--------------节点app2.js的输出-----

然而,当我在coffeescript中重现这个例子时,我只得到从子进程接收消息的父进程;显然,子进程没有从父进程接收消息

-----------app.coffee----------------------------

var child = require('child_process');
var forked = child.fork("./child2.js");

forked.on('message', function (msg) {
    console.log("parent recieved ", msg);
});

forked.send({hello:'world'});
process.on('message', function(m) {
    console.log("child received ", m);
});

process.send({foo:'bar'});
cp = require('child_process')
n = cp.fork("./child.coffee")

n.on 'message', (m) =>
    console.log 'PARENT recieved', m

n.send {foo:'hello from the parent process'}
process.on 'message', (m) =>
    console.log 'CHILD received ', m

console.log "Child process running"

process.send {bar:'hello from the child process'}
----------儿童咖啡---------------------------

var child = require('child_process');
var forked = child.fork("./child2.js");

forked.on('message', function (msg) {
    console.log("parent recieved ", msg);
});

forked.send({hello:'world'});
process.on('message', function(m) {
    console.log("child received ", m);
});

process.send({foo:'bar'});
cp = require('child_process')
n = cp.fork("./child.coffee")

n.on 'message', (m) =>
    console.log 'PARENT recieved', m

n.send {foo:'hello from the parent process'}
process.on 'message', (m) =>
    console.log 'CHILD received ', m

console.log "Child process running"

process.send {bar:'hello from the child process'}
--------------------咖啡app.coffee的输出----

查看已编译的javascript,我发现(正如预期的那样)编译coffeescript的结果基本上与原始javascript代码相同,只是包装在一个函数中,并对其进行了调用。这个问题一定是范围问题,但我看不出如何解决它。毫无疑问,这对上师来说将是微不足道的,我对它束手无策,所以我想我会问一下,以防有人好心给我指路


Darryn Reid博士。

不幸的是,这并不像范围界定问题那么简单。问题在于fork命令,在JS情况下,它将启动一个新的
节点
进程,而在CoffeeScript情况下,它将启动一个新的
咖啡
进程

如果没有大量的时间搜索,我不能肯定,但问题是它们的启动/编译时间不同,或者与之相关的东西

// Changing this line
n.send {foo:'hello from the parent process'}

// to this works for me
setTimeout (-> n.send {foo:'hello from the parent process'}), 300

我认为最简单的解决方案是,在从子进程获得一些初始的
'ready'
事件之前,不要向子进程发送任何消息。因此,您可以让您的
孩子。coffee
发送一些初始消息,告诉父进程它最终已编译并完全加载。

谢谢您的建议,我非常感谢。我猜这相当于“异步思考”;我认为node.js上的文档应该真正进行更改,以使其更加健壮。这并不是什么大变化,只是让父进程仅在从子进程接收消息后才发送消息。再次感谢。