使用用户界面管理异步Python
我正在建立一个涉及触摸屏用户界面的Raspberry Pi项目,并通过蓝牙低能读取温度传感器。我使用的是Python,Bleak Bluetooth库和Graphics.py Simple Graphics软件包。
graphics.py想要拥有事件循环。 Bleak与Asyncio深深相关,并希望生活在自己的Asyncio活动循环中。因此,我分叉了一个线程来照顾蓝牙内容,这些内容只是将其读数存放在全球变量中。一切都很好,直到用户按下退出按钮或某些错误终止程序。如果我不进行尝试并正确清理蓝牙物品,那么下次都不会找到该设备;在与该设备连接时,操作系统中更深的东西被卡住了,直到我重新启动为止。但这是在一个单独的线程中,因此将清理蓝牙的控制流并不明显。
是否有一种著名的方法来处理这种事情?一种众所周知的范式,用于将异步Python与图形用户界面混合?
I am building a Raspberry Pi project involving a touchscreen user interface, and reading temperature sensors via Bluetooth Low Energy. I'm using Python, the BLEAK Bluetooth library, and the graphics.py simple graphics package.
Graphics.py kind of wants to own the event loop. BLEAK is deeply involved with asyncio and wants to live in its own asyncio event loop. So I forked a thread to take care of the Bluetooth stuff, which simply deposits its readings in global variables. All well and good until the user presses a Quit button or some error terminates the program. If I don't do a try-finally and clean up the Bluetooth stuff properly, it won't even find the device the next time around; something deeper in the OS is jammed when it comes to connecting with that device, until I reboot. But it's in a separate thread, so the flow of control that is going to get that Bluetooth cleaned up is not obvious.
Is there a well-known method for dealing with this sort of thing? A well-known paradigm for mixing asynchronous Python with a graphical user interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为GUI运行事件循环并访问蓝牙是必经之路。
可以使用为设备记录的Bluez D-Bus API完成此操作:
https> https://git.kernel。 org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
和gatt特征:
https> https://git.kernel。 org/pub/scm/bluetooth/bluez.git/tree/doc/doc/gatt-api.txt
GTK库周围有一些合理的文档:
htttps://pygobject.readthedocs.io/en/en/latest/getting_startest/getting_started.html
和其他教程:
https://python-gtk-3-tutorial.readthecss。 IO/en/最终/install.html
以下是我拥有的设备的一个小示例,该设备在其温度上发出通知:
Running an event loop for the GUI and accessing Bluetooth is the way to go.
This can be done using the BlueZ D-Bus API that is documented for the device:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
And the GATT characteristics:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/gatt-api.txt
There is some reasonable documentation around the GTK library:
https://pygobject.readthedocs.io/en/latest/getting_started.html
And other tutorials:
https://python-gtk-3-tutorial.readthedocs.io/en/latest/install.html
Below is a small example for a device I have that gives notifications over BLE of its temperature: