-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Evaluate switching from txdbus to other Python DBus package #236
Comments
I think I would be in favor of exploring one of the alternatives. I did some work last year to fix some bugs in txdbus. One bug (that fortunately doesn't affect Bleak) I was not able to fix because it would require changing twisted, but I found it to just be too complicated. Both of the packages you have listed look good. |
I am running into some weird issues with Twisted right now that seem to be linked to this issue. It seems to be very related to the Here is the problem that I am seeing when trying to read a characteristic, for reference:
|
@cspensky Please try! I think Jeepney is more promising than DBussy, even though I like the name better of the latter one. |
is there any progress on that? I'm having the same issue but if notify callback is called of the chracteristics. would very much appreciate if this was solved soon. |
@mitea1 as far as I know, no effort has been spent trying to solve this yet. Please give it a go if you feel up for a challenge! |
@hbldh I'm afraid I will not find the time to dive deeply into this and solve it. @cspensky Do you know if this only happens on specific bluez versions. I'm using 5.50 and bleak 0.9.1. If there was a version without this issue I could downgrade to it |
@mitea1 I have been seeing this same issue, and have not found any solution (despite a reasonable amount of effort). My next step was going to be to switch the DBus package to fix it. Unfortunately, other things at work have taken my attention recently. I hope to have some time this month to address this issue. Please do let me know if you make any headway. For reference, I am running Bluez 5.48 and am on the |
@hbldh do I get that right if I say at the moment there is no valid version set bleak/bluez that works 100% without this issues regarding dbus? so only the .net backend version works? Just asking because I could probably switch to other bleak/bluez version if there is any that works without this issues |
No. I would not say that you are right. I have not personally experienced this in any of my usage of Bleak on Ubuntu, or at least not to any extent that has been annoying enough to warrant me remembering it. I have used a variety of BlueZ versions over the course of several years, but I have not registered which. Some Linux OS versions and BlueZ have more problems than others, see e.g. #332. But I have no record of which have worked well, since such errors have been isolated occurrences which have been solved by either identifying problematic hardware or downgrading BlueZ or Linux kernel. What do you mean 100%? That this will never happen? I cannot and will not guarantee that on any system. It is Bleak, plus several OS subsystems working together, combined with hardware, wireless communication and some connected peripheral with hardware and software of its own. Of course some strange disconnections will happen. Do not expect flawless execution with BLE.
No. I would say that all three backends "work". |
@cspensky I made an interesting observation: I use bleak partly to send (write) data to a "rx" characteristic and get responses from another "tx" characteristic which I subscribed to. This process is abstracted in a
I noticed that I get this error after every second call of Now I changed things a little bit After I connect to the device I call So
Now it works without any Issues. So to make things short: My assumption is that there is a problem for bluez if we use So the solution for me was just call @hbldh maybe this sounds legit to you or you might have an explanation for it? |
@mitea1 Thank you for the insights! I am also using a TX and RX characteristic with notifications. Based on your findings, I will see if I can try to get to the bottom of the issue. May I ask what type of device you are communicating with? I saw this when interacting with an iPhone XR. |
@cspensky Bleak is running inside a docker image
just out of curiosity... why are you using |
Another alternative could be "Bluetooth Management sockets" http://www.bluez.org/the-management-interface/. I do not think it is a good idea, lots of work etc, but it deserves to be mentioned. |
python-dbus-next is another good option. |
After playing with dbus-next for a bit, it seems to be a pretty heavyweight solution. 😞 Is anyone else seeing pretty high CPU usage, especially when scanning? I may poke around a bit and see if one of the other solutions to see if they are a bit more lightweight... Would love to get some input before I jump into this. |
Can you do some profiling to see what is using the CPU? I'm surprised that it would be much different from txdbus. |
I just ran a quick instance with cProfile where I ran a scanner until 3 devices were found and connected to.
|
Thanks for that. The Jeepney library specifically has performance as a non-goal, so I think that rules out that library. |
Unfortunately I used to contribute to PyBluez that used pygattlib, which was just a gatttool wrapper for Python. This doesn't appear to be has big of a problem on OSX, because it seems like CoreBluetooth does a lot of the heavy lifting with data processing. I have not had the opportunity test on Windows yet. |
Is performance important for advertising data, characteristic notifications or both?
Using such low-level calls requires elevated privileges and requires in-depth knowledge of the Bluetooth protocol and could potentially interfere with BlueZ that will still be running in the background anyway. For me personally, I think I would rather spend the time to fix BlueZ rather than trying to work around it. |
I'd love to have it be more performant all around. I'm not proposing going away from BlueZ but using a more direct interface. Something like gattlib's Python interface (pygattlib) should be significantly less overhead than using a pure-Python D-Bus solution. The library still seems to be maintained as well. |
Ah, I see I misunderstood how that library worked (based on the readme, it sounds like I'm not the only one). The only downside I see is that it can be a pain for users on non-x86 architectures to compile the library. But maybe it makes sense to add it as an optional backend for those who need the performance benift? |
This was the same conclusion that @Bernstern and I came to yesterday. I plan to add |
@cspensky I might have missed something in the discussion above, but I just want to weigh in with my opinion regarding the inclusion of gattlib in Bleak. I used to use Consequently, I created Bleak as an exercise in cross-platform API building and If you by some impressive feat of software engineering manage to coax My suggestion is that you make a new package and host it yourselves on PyPI. I would gladly include references to it in the Bleak docs and README, but since it relies on a "possibly-soon-deprecated" part of BlueZ and probably will violate some of the initial ideas of Bleak, I argue that it should be an external add-on in that case. Another option is to actually use You should, in my opinion, use Bleak for what it is written for (cross-platform without much hassle) and |
@hbldh Thank you for the detailed response. Indeed, I agree that Bleak is the of the various attempts that I have worked with, hence my current involvement in this repository. Thank you for all of your work! 🙏 I did start down the gattlib as a Bleak backend path out of curiosity but have since pulled the plug. For interested parties, it does seem to be possible to make it play nicely with async, you can see my branches here: bleak and https://github.com/Allthenticate/gattlib/tree/development. It appears that D-Bus in general is the bottleneck and not the Python code in particular. I ran some tests in pure C vs. the Python version and did not see much difference, unfortunately. The library that I was comparing the speed against was bluepy. However, it appears that are circumventing D-Bus and doing some hacky stuff where they are wrapping a compiled binary with Python to get that speed gain. The only way I've really found to get our CPU usage down is by adding scanning filters or, in our case, acting as a peripheral instead of a central. Thus, I plan to jump on board with bless and help push that forward. |
I am working on replacing Scanning works already, getting it to work was easy enough that I expect the rest to fall into place without too much hassle. Progress is somewhat limited by the fact that asyncdbus: https://github.com/M-o-a-T/asyncdbus, also on PyPI |
@smurfix that sounds great, but be advised that there already is work done on porting to |
Yeah, I saw. My problem here is that there's no good way to do this the anyio/Trio way without deprecating That being said, I learned (and copied) a lot from the code in the The cuff reader is in https://github.com/smurfix/ble-stuff. |
I plan to check with the dbus-next maintainers about merging my fork back, but (a) that'll have to wait for anyio v3 to be resonably stable/ready, and (b) as I wrote it does require some incompatible changes. |
I'm not sure this issue is the best place to mention it, but the current performance of the bleak with bluez backend is really poor.
This code consumes up to 60-70% running on imx6ull 680 MHz processor (arm32, single core). Similar results are both for txdbus and dbus-next based backends. I compared the results with the full-features software https://github.com/espruino/EspruinoHub bases on JS library "noble" for accessing BLE. CPU time is less than 3% for that software including scanning BLE and sending data to MQTT. I suppose that it would be hard to reach the same performance results using python-only dependencies. |
@devbis That is problematic indeed... Is it Bleak or the DBus system that consumes the CPU? If it is Bleak and not the DBus system, then I have too look at what actually takes time. Noble does not - as far as I know - use the DBus API of BlueZ, but implements its own solution, so it is not burdened by the overhead of DBus. It is an impressive feat and I have the outmost respect for the effort made by the developers to achieve such performance in Noble. However, I am not capable of nor interested in copying that for Bleak. Bleak was meant to be the opposite of Noble in a way; only use existing, built-in, OS native BLE solutions. Bleak will not implement communication of its own or regress to using old solutions. In case you want that, there is the pygattlib project that avoids DBus by doing something similar as Noble. |
@devbis Unfortunately, we had the same issues as well on both OSX and Linux. It appears to be a fundamental problem with parsing the high-throughput scanning data in Python, which is inherently slow. You can cut the CPU time a bit by filtering based on UUID or RSSI, for example. I submitted a fix a while back that fixed these on the dbus-next branch. However, it was still too CPU intensive for our needs. We now use Bleak for testing, but had to abandon it for anything more than poking around with devices. A fast, albeit EOL'd alternative, that has a C plugin and does not use D-Bus is https://github.com/IanHarvey/bluepy |
This change reduces the overhead of unmarshalling with the goal of having bleak scanners not overwhelming the system. See related issue: hbldh/bleak#236 (comment) ``` (speed_up_unmarsh) % python3 bench/unmarshall.py Unmarshalling 1000000 bluetooth rssi messages took 14.397348250000505 seconds (master) % python3 bench/unmarshall.py Unmarshalling 1000000 bluetooth rssi messages took 47.7756206660124 seconds ```
This change reduces the overhead of unmarshalling with the goal of having bleak scanners not overwhelming the system. See related issue: hbldh/bleak#236 (comment) ``` (speed_up_unmarsh) % python3 bench/unmarshall.py Unmarshalling 1000000 bluetooth rssi messages took 14.397348250000505 seconds (master) % python3 bench/unmarshall.py Unmarshalling 1000000 bluetooth rssi messages took 47.7756206660124 seconds ```
This change reduces the overhead of unmarshalling with the goal of having bleak scanners not overwhelming the system. See related issue: hbldh/bleak#236 (comment) ``` (speed_up_unmarsh) % python3 bench/unmarshall.py Unmarshalling 1000000 bluetooth rssi messages took 14.397348250000505 seconds (master) % python3 bench/unmarshall.py Unmarshalling 1000000 bluetooth rssi messages took 47.7756206660124 seconds ```
@cspensky If you get a chance it would be great to know if 0.19.0 resolves the performance concerns |
Unfortunately, I won't have the time to adequately test this for a bit. If
someone else wants to give it a try in the meantime, I would greatly
appreciate it. Otherwise, I can likely get around to comparing my runtimes
in the new year.
…On Wed, Oct 26, 2022 at 2:50 PM J. Nick Koston ***@***.***> wrote:
@cspensky <https://github.com/cspensky> If you get a chance it would be
great to know if 0.19.0 resolves the performance concerns
—
Reply to this email directly, view it on GitHub
<#236 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVJBSMAZQDZLAD4L6W3XS3WFGDSBANCNFSM4ONGVAJA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
*www.allthenticate.com <https://www.allthenticate.com/>*
*Chad Spensky, Ph.D. *
*Founder and CEO*
*(740) 632-6257LinkedIn <https://www.linkedin.com/in/chad-spensky/>Houston,
TX 77002*
|
Description
A problem with using txdbus as the solution to talk to the BlueZ DBus service is that it seems to leak file descriptors somewhere. Right now this is solved by having a store of twisted reactors in the
__init__.py
ofbleak.backends.bluez
.This might be improved by using another package. Candidates to evaluate:
Pros
asyncio
-only solution in LinuxCons
The text was updated successfully, but these errors were encountered: