-
Notifications
You must be signed in to change notification settings - Fork 121
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
Calling hub methods from callback functions blocks further operations #127
Comments
Hi I would suspect the |
Hmm, well from pylgbst.hub import MoveHub
from pylgbst import get_connection_bleak
conn = get_connection_bleak()
hub = MoveHub(conn)
def callback_btn(is_pressed):
global hub
print("Btn pressed: %s" % is_pressed)
if is_pressed == 0:
hub.motor_external.timed(5, 1.0)
try:
hub.button.subscribe(callback_btn)
time.sleep(60)
finally:
hub.button.unsubscribe(callback_btn)
hub.disconnect() Behavior is the same. Here's the debug output.
As you can see, right after
Then exited with exception once 60 seconds timer run out. |
This looks like the request for motor movement never gets the corresponding "ended" message. This might be a deadlock, when button click handler waits for command completion. It seems the motor completion message never gets through. The usual flow of processing info from hub is "DEBUG:comms-bleak:Response" followed by "DEBUG:hub:Notification". You can see it in the beginning of the log, but after "Btn pressed: 0" there's never "Notification" recognized. The fact that it happens is really strange and unusual. I looked over the code and not sure how can this happen. It would need some debugging to look at where each thread is, and track the problem. Unfortunately, I have no access to my MoveHub anymore, so I can't do that. If you are python-proficient and have proper IDE, you may try it. |
IMHO it is a totally bad idea to send a command in a callback function. You should raise some flag in the callback and look for it in another function with a circle. |
Hmm... great suggestion. I'll give it a try as soon as I can :) By circle you mean a loop right? |
Yes, a loop )) |
So, I tried to implement a simple demo where press of the button controls some other functions of a MoveHub, i.e. changes LED colour, or spins up a motor. The code looks like this:
Once I release the button for the first time, it spins up the motor for 5 seconds exactly as I expect it to do. However, it does seem to ignore any further button presses... callback function is not called at all as I am not even getting any strings printed. Is this an expected behaviour due to threaded execution?
Is there a way to implement this?
Thanks a lot in advance.
The text was updated successfully, but these errors were encountered: