问题描述
我在Raspberry Pi 3b上使用Python 3.7.3,并且试图确定最适合我的事件循环,还尝试了解asyncio和线程以及它们的用途。我目前在代码中使用QTimer()每隔1/2秒执行一次函数,该函数测试当前时间以查看是打开还是关闭鸡舍门的日出时间或黄昏。
使用time.sleep()
#!/usr/bin/python3
import time,sys
class main():
def __init__(self):
self.door_status = 'unknown'
self.run()
def open_door(self):
self.door_status = 'opening'
# it takes about 60 seconds to open the door
time.sleep(60)
self.door_status = 'open'
def close_door(self):
self.door_status = 'closing'
# it takes about 60 seconds to close the door
time.sleep(60)
self.door_status = 'closed'
def run(self):
while True:
"""
check time and open/close door as needed
if time to open door and door not opening or open
self.open_door()
elif time to close door and door not closing or closed
self.close_door()
"""
time.sleep(0.1)
# the follownig is to just let me know it's alive
time_fmt = "It's %I:%M:%S %p on %A,%b %d,%Y"
time_str = time.strftime(time_fmt)
sys.stdout.write(time_str)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write("\b"*len(time_str))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\n Processed Stopped")
"""
top -p 19905
top - 08:30:51 up 22 days,1:08,1 user,load average: 0.80,1.22,1.52
Tasks: 1 total,0 running,1 sleeping,0 stopped,0 zombie
%Cpu(s): 7.3 us,3.6 sy,0.0 ni,88.9 id,0.0 wa,0.0 hi,0.2 si,0.0 st
MiB Mem : 15930.8 total,3961.1 free,5104.3 used,6865.4 buff/cache
MiB Swap: 16271.0 total,15949.2 free,321.8 used. 9816.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
19905 john 20 0 16732 8296 5560 S 0.3 0.1 0:00.18 coop3t
"""
使用QTimer()
#!/usr/bin/python3
import time,sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer
class main(QApplication):
def __init__(self,*args,**kwargs):
super(main,self).__init__(*args,**kwargs)
self.door_status = 'unkown'
updateTimer = QTimer(self)
updateTimer.timeout.connect(self.run)
updateTimer.start(1000)
def open_door(self):
self.door_status = 'opening'
# it takes about 60 seconds to open the door
time.sleep(60)
self.door_status = 'open'
def close_door(self):
self.door_status = 'closing'
# it takes about 60 seconds to close the door
time.sleep(60)
self.door_status = 'closed'
def run(self):
while True:
"""
check time and open/close door as needed
if time to open door and door not opening or open
self.open_door()
elif time to close door and door not closing or closed
self.close_door()
"""
time.sleep(0.1)
# the follownig is to just let me know it's alive
time_fmt = "It's %I:%M:%S %p on %A,%Y"
time_str = time.strftime(time_fmt)
sys.stdout.write(time_str)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write("\b"*len(time_str))
if __name__ == '__main__':
try:
app = main(sys.argv)
app.exec_()
except KeyboardInterrupt:
print("\n Processed Stopped")
"""
john@d10cave:~$ top -p 20069
top - 11:44:52 up 22 days,4:22,load average: 1.15,1.34,1.41
Tasks: 1 total,0 zombie
%Cpu(s): 6.2 us,3.0 sy,90.8 id,0.1 si,3764.0 free,5222.0 used,6944.9 buff/cache
MiB Swap: 16271.0 total,321.8 used. 9695.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20069 john 20 0 441084 50252 36616 S 0.3 0.3 0:10.67 coop3q
"""
最大的问题是,在我的情况下,线程或异步会做任何事情来使程序功能更好吗?如果是这样,那么一个例子会很好。
谢谢 JT
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)