小编use*_*604的帖子

在CoffeeScript中使用child_process.send时缺少消息

我刚刚开始使用coffeescript,我喜欢它,但我遇到了令人沮丧的问题.当我在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'});
Run Code Online (Sandbox Code Playgroud)

---------- child2.js --------------------------------

process.on('message', function(m) {
    console.log("child received ", m);
});

process.send({foo:'bar'});
Run Code Online (Sandbox Code Playgroud)

--------------节点app2.js的输出-----

child received  { hello: 'world' }
parent recieved  { foo: 'bar' }
Run Code Online (Sandbox Code Playgroud)

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

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

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'}
Run Code Online (Sandbox Code Playgroud)

---------- child.coffee ---------------------------

process.on 'message', (m) =>
    console.log 'CHILD …
Run Code Online (Sandbox Code Playgroud)

process node.js coffeescript

3
推荐指数
1
解决办法
761
查看次数

标签 统计

coffeescript ×1

node.js ×1

process ×1