lobis / hvps

A Python package for controlling high voltage power supplies (HVPS) over serial port. Supports CAEN and iseg power supplies.
https://github.com/lobis/hvps
BSD 3-Clause "New" or "Revised" License
8 stars 2 forks source link

Make communication thread safe #38

Closed jherkenhoff closed 1 year ago

jherkenhoff commented 1 year ago

While using this library in the context of an EPICS server (using the p4p python library), which is inherently multi-threaded, I encountered issues while multiple threads tried to communicated with the device.

For example, wrapping the serial.write and serial.readline functions together in a threading.Lock would avoid any possible race conditions by making sure that no other thread can take over the serial bus during a "transaction".

What do you think about this? The problem would be: Where to store the threading.Lock?

lobis commented 1 year ago

Thanks for raising this, I am not 100% sure if this should be under the scope of this library but it's straightforward to implement, so we will add it for the time being.

If we were to continue with the current implementation we would have to pass the lock object multiple times to the method that sends the serial command. We could do that but I tought we could use this opportunity to refactor to reduce code duplication.

I created a PR (https://github.com/lobis/hvps/pull/39) to continue the conversation.

jherkenhoff commented 1 year ago

Sorry for my very late reply. I only had the chance to look into this now. It works perfect! No thread collisions at all, leading to much more stable operation of my device-server. Thanks a lot @lobis !