You can use asyncio.
This code is not tested, but should work.
You can decide if you kill the whole data collection process of data, if only one device fails.
Also it's a bit strange, that each esp is listening on a different port.
To simplify this, you could listem them all on the start port. Then the order does not matter.
If you need to know which esp has sent which data, the esp should send it's own id as unsigned short for example.
Then you don't need to enumerate all esps. In this case, you have to change the fortmat for struct a little bit (H on the beginning).
More ressources: https://docs.python.org/3/library/asynci...ml#streams
This code is not tested, but should work.
import asyncio import struct import csv async def connect_and_recv(ip, port): reader, writer = await asyncio.open_connection(ip, port) data = await reader.read(1024) writer.close() await writer.wait_closed() result = parse(data) return result async def collect_data(addresses): tasks = [connect_and_recv(ip, port) for ip, port in addresses] return await asyncio.gather(*tasks) def parse(data): integers = len(data) // 4 st_format = str(integers) + 'I' return struct.unpack(st_format, data) def write_data(data): with open('data.csv', 'w') as fd: writer = csv.writer(fd) writer.writerow(['id', 'data1', 'data2', 'data3', 'data4']) for index, row in enumerate(data): writer.writerow([index, *row]) STARTING_TCP_PORT= 5099 ips = [ '192.168.1.4', '192.168.1.3', '192.168.1.5', '192.168.1.6', '192.168.1.7', '192.168.1.8', '192.168.1.9', '192.168.1.10', '192.168.1.11', '192.168.1.12', '192.168.1.13', ] addresses = [(ip, STARTING_TCP_PORT + idx) for idx, ip in enumerate(ips)] loop = asyncio.get_event_loop() task = collect_data(addresses) print('Collecting data') data = loop.run_until_complete(task) print('Writing data') write_data(data) print('Finished')You should do something with exception handling.
You can decide if you kill the whole data collection process of data, if only one device fails.
Also it's a bit strange, that each esp is listening on a different port.
To simplify this, you could listem them all on the start port. Then the order does not matter.
If you need to know which esp has sent which data, the esp should send it's own id as unsigned short for example.
Then you don't need to enumerate all esps. In this case, you have to change the fortmat for struct a little bit (H on the beginning).
More ressources: https://docs.python.org/3/library/asynci...ml#streams
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!