简体   繁体   English

如何连续运行可以从节点接收命令的Python脚本

[英]How to run a Python script continuously that can receive commands from node

I have set up a Raspberry Pi connected to an LED strip which is controllable from my phone via a Node server I have running on the RasPi. 我已经建立了一个连接到LED灯条的Raspberry Pi,可以通过我在RasPi上运行的Node服务器从手机控制它。 It triggers a simple python script that sets a colour. 它会触发一个简单的设置颜色的python脚本。

I'm looking to expand the functionality such that I have a python script continuously running and I can send colours to it that it will consume the new colour and display both the old and new colour side by side. 我正在扩展功能,以使我不断运行python脚本,并且可以向其发送颜色,以使用新颜色并同时显示新旧颜色。 Ie the python script can receive commands and manage state. 即python脚本可以接收命令并管理状态。

I've looked into whether to use a simple loop or a deamon for this but I don't understand how to both run a script continuously and receive the new commands. 我已经研究了是否为此使用简单循环或守护进程,但是我不知道如何连续运行脚本并接收新命令。

Is it better to keep state in the Node server and keep sending a lot of simple commands to a basic python script or to write a more involved python script that can receive few simpler commands and continuously update the lights? 保持节点服务器中的状态并继续向基本python脚本发送许多简单命令,还是编写一个涉及更多的python脚本(可以接收一些简单命令并不断更新指示灯)的方法更好?

IIUC, you don't necessarily need to have the python script running continuously. IIUC,您不一定需要连续运行python脚本。 It just needs to store state, and you can do this by writing the state to a file. 它只需要存储状态,您可以通过将状态写入文件来做到这一点。 The script can then just read the last state file at startup, decide what to do from thereon, perform action, then update the state file. 然后,脚本可以在启动时仅读取最后一个状态文件,从中决定要做什么,执行操作,然后更新状态文件。

In case you do want to actually run the script continuously though, you need a way to accept the commands. 如果您确实希望连续不断地运行脚本,则需要一种接受命令的方法。 The simplest way for a daemon to accept command is probably through signal, you can use custom signal eg SIGUSR1 and SIGUSR2 to send and receive these notifications. 守护程序接受命令的最简单方法可能是通过信号,您可以使用自定义信号(例如SIGUSR1和SIGUSR2)发送和接收这些通知。 These may be sufficient if your daemon only need to accept very simple request. 如果您的守护程序只需要接受非常简单的请求,这些就足够了。

For more complex request where you need to actually accept messages, you can listen to a Unix socket or listen to a TCP socket. 对于需要实际接受消息的更复杂的请求,可以侦听Unix套接字或TCP套接字。 The socket module in the standard library can help you with that. 标准库中的套接字模块可以帮助您。 If you want to build a more complex command server, then you may even want to consider running a full HTTP server, though this looks overkill for the current situation. 如果要构建更复杂的命令服务器,则甚至可能要考虑运行完整的HTTP服务器,尽管这对于当前情况来说显得有些过头了。

Is it better to keep state in the Node server and keep sending a lot of simple commands to a basic python script or to write a more involved python script that can receive few simpler commands and continuously update the lights? 保持节点服务器中的状态并继续向基本python脚本发送许多简单命令,还是编写一个涉及更多的python脚本(可以接收一些简单命令并不断更新指示灯)的方法更好?

There's no straightforward answer to that. 没有直接的答案。 It depends on case by case basis, how complex the state is, how frequently you need to change colour, how familiar you are with the languages, etc. 这取决于具体情况,状态的复杂程度,更改颜色的频率,对语言的熟悉程度等。

Another option is to have the Node app, calll the Python script as a child process, and pass to it any needed vars, and you can read python's out put as well, like so: 另一个选择是使用Node应用程序,将Python脚本作为子进程调用,并将所需的var传递给它,您也可以读取python的输出,如下所示:

 var exec = require('child_process').exec;
 var child = exec('python file.py var1 var2', function (error, stdout, stderr) {
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM