简体   繁体   English

如何从Node.js调用Python函数?

[英]How do I call a Python function from Node.js?

I'm working on making a Homebridge plugin for a project. 我正在为一个项目制作Homebridge插件。 Homebridge is a Node.js server which I have running on a Raspberry Pi which emulates an Apple HomeKit Bridge. Homebridge是一台Node.js服务器,我已经在模仿Apple HomeKit Bridge的Raspberry Pi上运行。

Using this link, I was able to execute Python code from the following Node.js code: 使用链接,我能够从以下Node.js代码执行Python代码:

var Service, Characteristic;

var spawn = require('child_process').spawn;
var py = spawn('python', ['/home/pi/Desktop/RFbulb/nRF24L01PLUS.py']);
var data = [10,10,10];
var dataString = '';

var RFstatus = true;

module.exports = function(homebridge) {
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;

    homebridge.registerAccessory("homebridge-RFbulb", "RFbulb", RFbulbAccessory);
}

function RFbulbAccessory(log, config) {
    this.log = log;
    this.config = config;
    this.name = config["name"];
    this.address = config["address"];

    this.service = new Service.Lightbulb(this.name);
    this.service
        .getCharacteristic(Characteristic.On)
        .on('get', this.getOn.bind(this))
        .on('set', this.setOn.bind(this));
}

RFbulbAccessory.prototype.setOn = function(on, callback) { // This is the function throwing the error
    var state = on ? "on": "off";
    if (state == "on") {
        data = [1,parseInt(this.address, 10),100];
        dataString = '';
        py.stdout.on('data', function(data) {
            dataString += data.toString();
        });
        py.stdout.on('end', function() {
            console.log(dataString);
        });
        py.stdin.write(JSON.stringify(data));
        py.stdin.end();
        RFstatus = true;
    }
    callback(null);
}

RFbulbAccessory.prototype.getServices = function() {
    return [this.service];
}

Interestingly enough, when I activate the setOn function the first time (for example, to turn the device on) it works fine, but when I activate the setOn function a second time (to turn the device off) I get the following errors and the server exits: 有趣的是,当我第一次激活setOn函数(例如,打开设备)时,它可以正常工作,但是当我setOn激活setOn函数(以关闭设备)时,出现以下错误,并且服务器出口:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at writeAfterEnd (_stream_writable.js:166:12)
    at Socket.Writable.write (_stream_writable.js:211:5)
    at Socket.write (net.js:642:40)
    at RFbulbAccessory.setOn (/usr/lib/node_modules/homebridge-RFbulb/index.js:47:12)
    at emitThree (events.js:97:13)
    at emit (events.js:175:7)
    at Characteristic.setValue (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Characteristic.js:155:10)
    at Bridge.<anonymous> (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Accessory.js:710:22)
    at Array.forEach (native)
    at Bridge.Accessory._handleSetCharacteristics (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Accessory.js:655:8)

What could be causing this error? 是什么导致此错误? Especially since the function appears to work fine for a single use. 特别是因为该功能一次使用似乎可以正常工作。

You're getting that error because you're closing the input stream: 由于关闭输入流而收到该错误:

py.stdin.end();

After a stream has been closed, you can no longer write to it like you are here: 关闭流后,您将无法像在这里一样对其进行写操作:

py.stdin.write(JSON.stringify(data));

If the Python program you're running accepts multiple commands over STDIN then simply remove the py.stdin.end() line. 如果您正在运行的Python程序通过STDIN接受多个命令,则只需删除py.stdin.end()行。

However, it's likely that your Python program runs once then completes. 但是,您的Python程序很可能会运行一次然后完成。 If that's the case, you will need to respawn the process every time you want the program to run. 如果是这样,您每次需要运行程序时都需要重新生成该进程。

if (state === "on") {
    py = spawn('python', ['/home/pi/Desktop/RFbulb/nRF24L01PLUS.py']);
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (node.js) 我如何调用这个中间件 function - (node.js) How do I call upon this middleware function 如何从node.js中的函数调用结构内部的函数? - How do I call a function inside a struct from that function in node.js? 如何在需要return语句的GraphQL解析器中调用异步node.js函数? - How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement? 如何在 node.js 文件中编写 function,然后在常规 js 文件中调用该 function - How do I write a function in a node.js file, and then call that function in a regular js file 通过node.js运行静态文件时,如何调用node.js文档中定义的函数? - When running static files through node.js, how do I call a function defined in the node.js document? 从 node.js 调用 python sql function 或反应 - Call a python sql function from node.js or react 在 Node.js 中,如何让一台服务器调用另一台服务器上的函数? - In Node.js, how do I make one server call a function on another server? 如何从 HTML 页面调用 node.js 函数? - How do you call a node.js function from a HTML page? 我该如何称呼这个 node.js function? - How should I call this node.js function? 如何从node.js中的导出模块调用函数? - How to call a function from a exported module in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM