bash 向 Python 脚本发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28813210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send message to a Python Script
提问by EffegiWeb
I'm trying to write a little python program for shutdown or Reboot my Raspberry PI, drived by a button connected to an GPIO. The program can show the current status of the raspberry PI (Booting,Running,Halting,Rebooting) via two leds. The python program is executed as daemon, started by a init.d bash script (written using the /etc/init.d/skeleton).
我正在尝试编写一个用于关闭或重新启动我的 Raspberry PI 的 Python 小程序,由连接到 GPIO 的按钮驱动。该程序可以通过两个 LED 显示 raspberry PI 的当前状态(启动、运行、暂停、重新启动)。python 程序作为守护进程执行,由 init.d bash 脚本(使用 /etc/init.d/skeleton 编写)启动。
Now I can start/stop/verify the status of the daemon, and the daemon can check the input where the button is connected, to perform the command "shutdown -h now" or "shutdown -r now" .
现在我可以启动/停止/验证守护进程的状态,守护进程可以检查按钮连接的输入,以执行命令 "shutdown -h now" 或 "shutdown -r now" 。
For show the current status of the raspberry PI, I had thought of send messages to the daemon, using some script in the runlevels directorys, for change the status of the leds. But I don't know how receive message in the python program.
为了显示 raspberry PI 的当前状态,我想到了使用 runlevels 目录中的一些脚本向守护程序发送消息,以更改 LED 的状态。但我不知道如何在 python 程序中接收消息。
Someone can help me?
有人可以帮助我吗?
Thanks.
谢谢。
回答by Patxitron
There are several methods to send a message from one script/app to another:
有多种方法可以将消息从一个脚本/应用程序发送到另一个脚本/应用程序:
For you application a valid method is to use a named pipe. Create it using os.mkfifo, open it read-only in your python app and then wait for messages on it.
对于您的应用程序,一个有效的方法是使用命名管道。使用os.mkfifo创建它,在你的 python 应用程序中以只读方式打开它,然后等待它上面的消息。
If you want your app to do another things while waiting, I reccomend you open the pipe in non-blocking mode to look for data availability without blocking your script as in following example:
如果您希望您的应用程序在等待时做其他事情,我建议您以非阻塞模式打开管道以查找数据可用性而不阻塞您的脚本,如下例所示:
import os, time
pipe_path = "/tmp/mypipe"
if not os.path.exists(pipe_path):
os.mkfifo(pipe_path)
# Open the fifo. We need to open in non-blocking mode or it will stalls until
# someone opens it for writting
pipe_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
with os.fdopen(pipe_fd) as pipe:
while True:
message = pipe.read()
if message:
print("Received: '%s'" % message)
print("Doing other stuff")
time.sleep(0.5)
Then you can send messages from bash scripts using the command
然后你可以使用命令从 bash 脚本发送消息
echo "your message" > /tmp/mypipe
echo "your message" > /tmp/mypipe
EDIT:I can not get select.select working correctly (I used it only in C programs) so I changed my recommendation to a non-bloking mode.
编辑:我无法让 select.select 正常工作(我只在 C 程序中使用它)所以我将我的建议更改为非阻塞模式。
回答by EffegiWeb
Is not more convenient this version?
With the with
costruct inside the while true:
loop?
In this way, all other code inside the loop is executable even in case of error in the pipe file management. Eventually I can use the try:
costuct
for catching the error.
这个版本不是更方便吗?with
在while true:
循环内使用costruct吗?这样,即使在管道文件管理出错的情况下,循环内的所有其他代码也是可执行的。最终我可以使用try:
costuct 来捕捉错误。
import os, time
pipe_path = "/tmp/mypipe"
if not os.path.exists(pipe_path):
os.mkfifo(pipe_path)
# Open the fifo. We need to open in non-blocking mode or it will stalls until
# someone opens it for writting
pipe_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
while True:
with os.fdopen(pipe_fd) as pipe:
message = pipe.read()
if message:
print("Received: '%s'" % message)
print("Doing other stuff")
time.sleep(0.5)