【发布时间】:2019-01-22 16:08:39
【问题描述】:
我正在读取 1 到 4 个设备,我连接到每个设备并启动一个线程来读取数据。 我想创建一个运行线程 unit1、unit2 等然后再次启动的 while 循环。我都需要设置一个计时器,每 5 秒读取一次所有设备。
我的代码用于读取和发送数据我在理解如何创建一个 while 循环来永久读取单位时遇到问题。
thread_list = []
ports = ['/dev/ttyXRUSB0']
for item in ports:
#setup conection
client = EPsolarTracerClient(method = 'rtu', port = item, baudrate = 115200, timeout = 0.2 )
#client = ModbusClient(method = 'rtu', port = item, baudrate = 115200, timeout = 0.2 )
client.connect()
#read config from device
epstime = client.read_rtc()
sync_date = datetime.datetime.now()
#write config to device
client.write_rtc(sync_date)
#create and start thread for device
thread = threading.Thread(target=readunit,args=(client,))
thread_list.append(thread)
thread.start()
# below is just leftover from old code but it is where i want to start the loop
while client.connect:
# readunit(client) # old code
sleepTime = 5
sleep(sleepTime)
我需要每 X 秒运行一次每个线程。并重复 永远.. 例如
线程1 线程2 …… 睡眠 x 秒。 重复
【问题讨论】:
标签: python multithreading python-2.7