dronekit / dronekit-python

DroneKit-Python library for communicating with Drones via MAVLink.
https://readthedocs.org/projects/dronekit-python/
Apache License 2.0
1.63k stars 1.45k forks source link

About the attribute listener function #806

Open vincentwen1995 opened 6 years ago

vincentwen1995 commented 6 years ago

Hi, all. Recently I encountered a problem of monitoring the script and starting/pausing it accordingly. As written in the developer's guide, a listener function on attributes can be created. So I tried with monitoring the flight mode, and pause the script with a while loop when the flight mode is not GUIDED, yet, during testing, this turned out to be messy. It seems that the listener function is invoked in a new thread, and the while loop is therefore running in that new thread, in parallel with the main thread. So I am curious whether somebody met with similar problems and have some experience to realize it?

pietrodn commented 6 years ago

The attribute callbacks are indeed called from a separate thread, because they are triggered by MAVLink message listeners (example), which in turn are called from the mavlink_thread_in function, executed in a separate thread.

You should never do long-running tasks in those callbacks, otherwise you'll block the main MAVLink receiving thread.