V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
monkeycz
V2EX  ›  Node.js

python.js ,一个在 node.js 中调用 Python 代码的模块

  •  1
     
  •   monkeycz · 2015-03-20 17:37:26 +08:00 · 3932 次点击
    这是一个创建于 3316 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前一段时间用 node.js 做项目,感觉 node.js 在服务器方面游刃有余,但是其他方面难望 Python 之项背。于是撸了一个可以在 node.js 中调用 Python 代码的模块。支持 node.js ,io.js , nw.js 。

    例子如下:

    var python = require('python.js');
    var os = python.import('os');
    
    var path = require('path');
    
    assert(os.path.basename(os.getcwd()) == path.basename(process.cwd()));
    

    支持以异步模式调用 Python 的代码:

    def test():
        try:
            ## do samething
        except Exception as e:
            raise e
        return 'done'
    
    PYMODULE.test.async = true;
    PYMODULE.test(function (result, error) {
        if (!error)
            console.log(result);
    });
    

    最后放上项目地址:
    https://github.com/monkeycz/python.js

    3 条回复    2015-03-21 14:59:39 +08:00
    ugmc
        1
    ugmc  
       2015-03-20 18:34:55 +08:00 via Android
    挺好
    gamexg
        2
    gamexg  
       2015-03-21 09:33:42 +08:00
    生产环境用起来怎么样?
    现在我 nodejs 调 python 是通过 rest.post(url) 来使用的。
    monkeycz
        3
    monkeycz  
    OP
       2015-03-21 14:59:39 +08:00
    @gamexg 目前手边有两个已发布的项目在用这个模块。项目里 node.js 和 Python 的代码比例都是接近 1:1,通过 python.js 进行桥接。暂时没发现什么问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5667 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:03 · PVG 14:03 · LAX 23:03 · JFK 02:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.