热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

nodejs代替python,NodeJS派生的Python进程Python的process.send()的替代方案?

ImforkingaPythonscriptwithNodeJSandwhenforked,bydefault,NodeJScreateanIPCbetweenthisnewpro

I'm forking a Python script with NodeJS and when forked, by default, NodeJS create an IPC between this new process and the parent.

With NodeJS, to send message from a child to the parent I do process.send({msg : 'toto'})

How can I do that with Python ?

解决方案

Ok I found it, finally is quite easy. It's only about writing on the right file descriptor.

On the NodeJS side parameter, spawn your script like that :

var child = child_process.spawn('python', ['hello.py'], {

stdio:[null, null, null, 'ipc']

});

child.on('message', function(message) {

console.log('Received message...');

console.log(message);

});

As the 'ipc' channel is the 4rd parameter, you will have to write on the filedescriptor 3.

On the Python side :

import os

os.write(3, '{"dt" : "This is a test"}\n', "utf8")

Done. You will receive the message on the child.on('message' callback.

Cheers !



推荐阅读
author-avatar
伪祢添睬_362
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有