将消息发送到Python脚本
发布时间:2020-12-16 23:44:15 所属栏目:Python 来源:网络整理
导读:我正在尝试编写一个用于关闭的小 python程序或重新启动我的Raspberry PI,由连接到GPIO的按钮驱动.该程序可以通过两个LED显示树莓PI(引导,运行,暂停,重新启动)的当前状态. python程序作为守护进程执行,由init.d bash脚本(使用/etc/init.d/skeleton编写)启动.
我正在尝试编写一个用于关闭的小
python程序或重新启动我的Raspberry PI,由连接到GPIO的按钮驱动.该程序可以通过两个LED显示树莓PI(引导,运行,暂停,重新启动)的当前状态.
python程序作为守护进程执行,由init.d bash脚本(使用/etc/init.d/skeleton编写)启动. 现在我可以启动/停止/验证守护程序的状态,守护程序可以检查按钮连接的输入,执行命令“shutdown -h now”或“shutdown -r now”. 为了显示raspberry PI的当前状态,我曾想过使用runlevels directorys中的一些脚本向守护进程发送消息,以更改leds的状态. 有人可以帮帮我吗? 谢谢. 解决方法
有几种方法可以将消息从一个脚本/ app发送到另一个脚本/ app:
对于您的应用程序,一个有效的方法是使用命名管道.使用os.mkfifo创建它,在python应用程序中以只读方式打开它,然后等待它上面的消息. 如果您希望应用程序在等待时执行其他操作,我建议您以非阻塞模式打开管道以查找数据可用性,而不会阻止您的脚本,如下例所示: 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) 然后,您可以使用该命令从bash脚本发送消息 回声“你的消息”>的/ tmp / mypipe 编辑:我不能让select.select正常工作(我只在C程序中使用它)所以我把我的推荐改为非bloking模式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |