pymavlink in multi-threaded environment

51 views
Skip to first unread message

Dmytro Shkarupa

unread,
Mar 2, 2025, 3:04:45 PMMar 2
to MAVLink
Hi, guys! Could you please provide an example of using the pymavlink connection in multi-threaded environment? I am particularly interested in receiving MAVLink messages in one thread and sending them from the second thread. Is pymavlink connection thread-safe? If no (I expect that it is not), should I use threading.Lock before both "recv_match" and "send" function calls in different threads? Have someone tried such an approach? If yes, could you please tell how much overhead it introduces?

Steven Owens

unread,
Mar 3, 2025, 8:23:05 PMMar 3
to MAVLink
I'm pretty sure the mavlink_connection object is not thread safe. 

The big gotcha is that you have to:

1) frequently send the heartbeat at least once a second, and also

2) frequently call recv_match() to keep the network buffer from being filled up, even if you're not listening for a partiuclar message, just call recv_match() and ignore what it returns.. 

I've been meaning to write up a multithreaded pymavlink client, but I haven't yet.  My general plan was/is to have a separate thread that does all the mavlink messaging and the rest of the code talks to it via a pair of thread-safe queues, one for outgoing mavlink messages, one for incoming mavlink messages.  The mavlink messaging thread would just keep looping, sending the heartbeat, checking the outbound queue for outgoing messages from the rest of the app, calling recv_match() and dropping any incoming messages on the inbound queue.

I wrote up some rough notes on working with pymavlink last year, you may find them useful:

https://www.darksleep.com/mavlink/pymavlink/

Steve

Santiago Perez

unread,
Mar 9, 2025, 4:50:33 PMMar 9
to MAVLink
Hello Steve,

Your multithreaded pymavlink client project sounds cool and like something I would be very interested in. Have you ever messed around with the Dronekit API? I've been looking at the code, so when you mentioned your client, it reminded me of it... are they similar? In the dronekit repo, there's a file called mavlink.py, so I was wondering if your client would be implemented similar to that? In any case, I would be open to contributing to your client, so I'll be on the lookout for any news regarding it in the future. Have a great one!

Best,
Santiago

Steven Owens

unread,
May 10, 2025, 1:35:30 AMMay 10
to MAVLink
Hi again,

I haven't messed with dronekit, I keep meaning to go through the dronekit code with microscope and tweezers, purely as a learning project.  Dronekit definitely sounds like something I'm interested in, but it's also been a few years since it had any code updates.  If/when I reach a point of familiarity with pymavlink where I feel confident, I might try to volunteer on dronekit.

I'll take a look at mavlink.py, thanks.

I just did literally the first pass at writing a multithreaded pymavlink client (working title is "mavthread").  I haven't even run it against a simulation yet.  I have a pretty busy the next week or two but I'll try to follow up here after I've done some testing and found the obvious bugs.

Steve
Reply all
Reply to author
Forward
0 new messages