You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Writing to characteristic values from outside the event loop #5
I'm not sure how to make use of the event loop properly:
I would like to use the GATT SDK inside a flask server. When a http request is sent to the server I would like to write a value to a characteristic (by handle). But how can I access the DeviceManager or even start / stop it once it is running forever?
Run manager.run() in another thread or process, then you can stop it with manager.stop().
If you run it in a thread, it should be easy to access the device class members so you'll be able to use the services/characteristics once they're resolved.
Did you actually try that? I did. It works but has some odd side-effects like the thread running the manager then receiving exceptions like ctrl-c and not the main thread anymore.
I think a more detailed example would sure be helpful.
Yeah, I tried it and didn't have any problems. @its2loud, I edited your example a little to show you how I did it. The manager.run() function is the blocking one so that's the only thing I ran in the thread. Try this?
If i replace the call to manager.run() by something trivial like a time.sleep() then the exception handling works as expected. So manager.run() somehow influences all this.
Thank you very much. This solves the Issue. Now ctrl + C does not stop the Device Manager Thread anymore but raises a KeyboardInterrupt in the Main Thread as it is supposed to.
Activity
swojo commentedon Jun 30, 2017
Run manager.run() in another thread or process, then you can stop it with manager.stop().
If you run it in a thread, it should be easy to access the device class members so you'll be able to use the services/characteristics once they're resolved.
harbaum commentedon Jul 6, 2017
Did you actually try that? I did. It works but has some odd side-effects like the thread running the manager then receiving exceptions like ctrl-c and not the main thread anymore.
I think a more detailed example would sure be helpful.
its2loud commentedon Jul 6, 2017
i tried running it in another thread as well:
https://gist.github.com/its2loud/93f65c27c74df8a934067d7c330cef18
unfortunately i wasn't able to stop it then. any idea, what i am doing wrong?
an example would be very useful of course.
swojo commentedon Jul 6, 2017
Yeah, I tried it and didn't have any problems.
@its2loud, I edited your example a little to show you how I did it. The manager.run() function is the blocking one so that's the only thing I ran in the thread. Try this?
https://gist.github.com/swojo/2b9b45cdd3bc744e28f7a2fbe885eace
harbaum commentedon Jul 6, 2017
Thanks a lot. But as commented in your example i still see the manager thread receiving the ctrl-c and not the main thread.
harbaum commentedon Jul 7, 2017
I see this on two different ubuntu x86 machines and on a raspberry pi. On what kind of machin does this work for you?
its2loud commentedon Jul 7, 2017
As commented below the gist I can't even stop it with Ctrl+C. It takes a few seconds and throws another exception..
harbaum commentedon Jul 7, 2017
If i replace the call to manager.run() by something trivial like a time.sleep() then the exception handling works as expected. So manager.run() somehow influences all this.
harbaum commentedon Jul 10, 2017
The closest thing i could come up with is this:
https://gist.github.com/harbaum/db0a0928458fefc0793df66a5d377b16
But it e.g. requires the main thread to actively check for the manager thread to be still alive.
Here someone describes exactly the same problem:
https://stackoverflow.com/questions/36680667/terminate-gobject-mainloop-threads-together-with-main/36778891
Interestingly he accepts the answer although it doesn't help in this case as the signal handler of the main thread never receives the signal.
harbaum commentedon Jul 10, 2017
This one also describes the same problem:
https://stackoverflow.com/questions/28465611/python-dbusgmainloop-inside-thread-and-try-and-catch-block/28476630
The solution presented there also doesn't really solve the problem but rather works around it.
Rewrite CLI to counteract BUG in gatt getsenic/gatt-python#5
Rewrite CLI to counteract BUG in gatt getsenic/gatt-python#5
Fix: can't handle sigint when running gatt.DeviceManager outside the …
Snevzor commentedon Mar 21, 2018
@harbaum, can you confirm #28 fixes your issue?
netding commentedon Jun 7, 2018
Thank you very much. This solves the Issue. Now ctrl + C does not stop the Device Manager Thread anymore but raises a KeyboardInterrupt in the Main Thread as it is supposed to.