frappy/frappy_psi/pyanc350.py
Markus Zolliker 1715f95dd4 frappy_psi.attocube: add lock protection to hw access
in order to avoid sporadic timeout problems

Change-Id: I36f67ae72b65e9c1f3179cae942b0a7d94584e55
2024-03-27 17:08:24 +01:00

46 lines
1.2 KiB
Python

"""wrapper around PyANC350v4
main purpose: make Positioner thread safe
without this, when accessing the device from different threads, timeouts may appear
allows also to omit axis in method arguments
"""
import inspect
import threading
# make sure PYTHONPATH points to the directory of PyANC350v4.py
# e.g. in /home/l_samenv/Documents/anc350/Linux64/userlib/lib
from PyANC350v4 import Positioner
class HwAxis:
hw = None
lock = None
axes = set()
def __init__(self, axisNo):
self.axisNo = axisNo
if not self.axes:
self.axes.add(axisNo)
HwAxis.hw = Positioner()
HwAxis.lock = threading.Lock()
super().__init__()
def close(self):
self.axes.discard(self.axisNo)
if not self.axes:
self.hw.disconnect()
# wrap methods from Positioner and insert to HwAxis
for name, method in Positioner.__dict__.items():
try:
arguments = inspect.getfullargspec(method).args
except TypeError as e:
continue
if arguments[0:2] == ['self', 'axisNo']:
def wrapper(self, *args, _method=method, **kwds):
with self.lock:
return _method(self.hw, self.axisNo, *args, **kwds)
setattr(HwAxis, name, wrapper)