ljean / modbus-tk

Create Modbus app easily with Python
Other
570 stars 212 forks source link

Allow caller to optionally override @threadsafe_function locking. #63

Closed bbarcklay closed 7 years ago

bbarcklay commented 8 years ago

This change gives the caller the ability to override the @threadsafe_function decorator by calling the decorated function, like execute(), with the kwarg 'threadsafe=False'. This is useful when the client code is managing multiple modbus.Master instances, each in their own thread, and wants to avoid the global locking that takes place.

Here is psuedocode of what I am executing without error:


def modbus_worker(modbus_master):
   result = modbus_master.execute(..., threadsafe=False)
   print(result)

modbus_threads = []
for ip, port in devices:
   master = modbus_tcp.Master(host=ip, port=port)
   t = threading.Thread(target=modbus_worker, args=(master,))
   modbus_threads.append(t)
   t.start()

The default behavior is unchanged. Callers must include the new 'threadsafe' kwarg to avoid locking.