I have some hardware devices in my network that I need to read from them data every 100ms and I need some async way to do it and not to wait to each call.
one way to do it is to use threads and other is using asyncio that uses loop.run_executer method(that create thread for each call). In both cases it will be async so I really don't understand what asyncio is giving us that threads are not.
can someone explain what is the advantage of using asyncio and not threads?
For example How can I turn the next code to be asyncio code:
def _send(self, data):
"""Send data over current socket
:param data: registers value to write
:type data: str (Python2) or class bytes (Python3)
:returns: True if send ok or None if error
:rtype: bool or None
"""
# check link
if self.__sock is None:
self.__debug_msg('call _send on close socket')
return None
# send
data_l = len(data)
try:
send_l = self.__sock.send(data)
except socket.error:
send_l = None
# handle send error
if (send_l is None) or (send_l != data_l):
self.__last_error = const.MB_SEND_ERR
self.__debug_msg('_send error')
self.close()
return None
else:
return send_l
This code is taken from ModbusClient class
Thanks