为不同的操作分隔 time.sleep
Separating time.sleep for different actions
这里 python 相当新,并试图将 time.sleep 函数分离出来用于多个读数。
while True:
#read from a analog sensor on input 1
d= grovepi.analogRead(1)
#read from an analog sensor on input 2
a= grovepi.analogRead(2)
#read from an digital sensor on input 3
(t,h)=grovepi.dht(3,0)
#output the data
#print (datetime,time)
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
print ("Noise:",d,"dB")
print ("Light:",a,"lux")
print ("Temperature:",t,"C")
print ("Humidity:",h,"rH")
grovelcd.setText("T:" + str(t) + " H:" + str(h) + " N:" + str(d)+ " L:" + str(a))
time.sleep(5)
我想要以不同的频率打印读数,但仍然 运行 同时。
例如
print ("Noise:",d,"dB")
time.sleep(3)
和
print ("Light:",a,"lux")
time.sleep(5)
我知道这可能是一个简单的语法问题,但我还没有找到简单的解决方案。
非常感谢
这是使用线程的部分解决方案:
import threading
import time
import grovepi
def take_analog_measurement(stop, lock, name, pin, units, period):
while not stop.is_set():
with lock:
val = grovepi.analogRead(pin)
print(name, ': ', val, units, sep='')
time.sleep(period)
def take_dht_measurement(stop, lock, pin, mtype, period):
while not stop.is_set():
with lock:
temp, hum = grovepi.dht(pin, mtype)
print('Temperature: ', temp, ' C\nHumidity: ', hum, ' rH', sep='')
time.sleep(period)
stop = threading.Event()
grovepilock = threading.Lock()
threads = []
threads.append(threading.Thread(target=take_analog_measurement, args=(stop, grovepilock, 'Noise', 1, 'dB', 3)))
threads.append(threading.Thread(target=take_analog_measurement, args=(stop, grovepilock, 'Light', 2, 'lux', 5)))
threads.append(threading.Thread(target=take_dht_measurement, args=(stop, grovepilock, 3, 0, 7)))
for thread in threads:
thread.start()
for thread in threads:
try:
thread.join()
except KeyboardInterrupt:
stop.set()
thread.join()
print('done')
我没有 GrovePi,所以无法用硬件测试它,但我做了一些模拟测试。
这将以给定频率读取每个传感器并输出值。该锁用于保护 grovepi
因为我不确定它是否是线程安全的。该事件用于通知所有线程停止(尽管它们必须等到它们醒来才能真正停止)。
当每个变量以不同的频率变化时,我不知道你想如何处理 grovelcd.setText
。一种可能的解决方案可能是使用与所有传感器线程共享的字典(和锁)的另一个线程。然后 LCD 会更新一段时间并使用字典中的数据。
这里 python 相当新,并试图将 time.sleep 函数分离出来用于多个读数。
while True:
#read from a analog sensor on input 1
d= grovepi.analogRead(1)
#read from an analog sensor on input 2
a= grovepi.analogRead(2)
#read from an digital sensor on input 3
(t,h)=grovepi.dht(3,0)
#output the data
#print (datetime,time)
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
print ("Noise:",d,"dB")
print ("Light:",a,"lux")
print ("Temperature:",t,"C")
print ("Humidity:",h,"rH")
grovelcd.setText("T:" + str(t) + " H:" + str(h) + " N:" + str(d)+ " L:" + str(a))
time.sleep(5)
我想要以不同的频率打印读数,但仍然 运行 同时。
例如
print ("Noise:",d,"dB")
time.sleep(3)
和
print ("Light:",a,"lux")
time.sleep(5)
我知道这可能是一个简单的语法问题,但我还没有找到简单的解决方案。
非常感谢
这是使用线程的部分解决方案:
import threading
import time
import grovepi
def take_analog_measurement(stop, lock, name, pin, units, period):
while not stop.is_set():
with lock:
val = grovepi.analogRead(pin)
print(name, ': ', val, units, sep='')
time.sleep(period)
def take_dht_measurement(stop, lock, pin, mtype, period):
while not stop.is_set():
with lock:
temp, hum = grovepi.dht(pin, mtype)
print('Temperature: ', temp, ' C\nHumidity: ', hum, ' rH', sep='')
time.sleep(period)
stop = threading.Event()
grovepilock = threading.Lock()
threads = []
threads.append(threading.Thread(target=take_analog_measurement, args=(stop, grovepilock, 'Noise', 1, 'dB', 3)))
threads.append(threading.Thread(target=take_analog_measurement, args=(stop, grovepilock, 'Light', 2, 'lux', 5)))
threads.append(threading.Thread(target=take_dht_measurement, args=(stop, grovepilock, 3, 0, 7)))
for thread in threads:
thread.start()
for thread in threads:
try:
thread.join()
except KeyboardInterrupt:
stop.set()
thread.join()
print('done')
我没有 GrovePi,所以无法用硬件测试它,但我做了一些模拟测试。
这将以给定频率读取每个传感器并输出值。该锁用于保护 grovepi
因为我不确定它是否是线程安全的。该事件用于通知所有线程停止(尽管它们必须等到它们醒来才能真正停止)。
当每个变量以不同的频率变化时,我不知道你想如何处理 grovelcd.setText
。一种可能的解决方案可能是使用与所有传感器线程共享的字典(和锁)的另一个线程。然后 LCD 会更新一段时间并使用字典中的数据。