问题描述
我不是编程专家,因此我在Google上进行了大量搜索,以使该脚本可以正常工作。它在串行接口上侦听ans正在搜索3个值(温度,湿度和电池电量)。如果找到zhem之一,则将其保存到文本文件中,并检查该值是否在某个级别之上或之下。如果是这种情况,它将发送一封电子邮件进行警告。
我的问题是,它会同时使用约99%的cpu功率...
您能帮我将CPU使用率限制到最低吗?
谢谢
#!/ usr / bin / env python
#-*-编码:utf-8-*-
导入序列
导入时间
导入系统
导入smtplib
从时间导入睡眠
def邮件(种类,方式,值,单位):
fromaddr ='[email protected]'
toaddrs ='[email protected]'
msg = \r\n .join([
发件人:发件人,
发件人:收件人,
主题:警告,
,
+ str(种类)+太 + str(方式)+。它是 + str(值)+ str (单位)
])
用户名='用户'
密码='密码'
服务器= smtplib.SMTP('server:port')
server.ehlo ()
server.starttls()
server.login(用户名,密码)
server.sendmail(fromaddr,toaddrs,msg)
server.quit()
def main():
端口='/ dev / ttyAMA0'
波特= 9600
ser = serial.Serial(端口=端口,波特率=波特)
sleep(0.2)
而True:
而ser.inWaiting():
#读取单个字符
char = ser.read()
#检查是否有LLAP消息的开始
如果char =='a':
#通过添加'a'开始构建完整的llap消息,我们有
llapMsg ='a'
#从接下来的11个字符中读取串行缓冲区
## $ b llapMsg + = ser.read(11)
如果llapMsg中的 TMPB:
TMPB = llapMsg [7:]
with open( TMPB .txt, w)作为text_file:
text_file.write(TMPB)
,如果float(TMPB)> = 19:
mail(温度,高,TMPB,°C)
elif float(TMPB)≤15:
mail(温度,低,TMPB,°C )
else:
通过
elif llapMsg中的 HUMB:
HUMB = llapMsg [7:]
with open( HUMB.txt , w)作为text_file:
text_file.write(HUMB)如果float(HUMB)> = 80,则
:
mail(湿度,高,HUMB, %)
elif float(HUMB)< = 70:
邮件(湿度,低,HUMB,%)
其他:
通过
elif在llapMsg中的 BATT:
BATT = llapMsg [7:11]
以open( BATT.txt, w)作为text_file:
text_file .write(BATT)
如果float(BATT)< 1:
邮件(电池电量,低,BATT, V)
其他:
通过
睡眠(0.2)
if __name__ == __main__:
main()
我自己解决了这个问题。
而ser.inWaiting():
循环造成了沉重的cpu负载。
我删除了它并纠正了缩进,它在cpu负载很小的情况下也很好用。
感谢您的提示,它帮助我解决了问题。 / p>
I'm not an expert in programming so i googled a lot to get this script to work. It listens on the serial interface ans is searching for 3 values (temperature, humidity and battery level). If it finds one of zhem it saves it to a text file and checks if the value is above or under a certain level. I f this is the case it sends an e-mail to warn.
My problem is that it uses constatntly about 99% of cpu power...Can you help me to limit the CPU usage to a minimum.
Thanks
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import sys
import smtplib
from time import sleep
def mail(kind, how, value, unit):
fromaddr = '[email protected]'
toaddrs = '[email protected]'
msg = "\r\n".join([
"From: sender",
"To: recipient",
"Subject: Warning",
"",
"The " + str(kind) + " is too " + str(how) + ". It is " + str(value) + str(unit)
])
username = 'user'
password = 'password'
server = smtplib.SMTP('server:port')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
def main():
port = '/dev/ttyAMA0'
baud = 9600
ser = serial.Serial(port=port, baudrate=baud)
sleep(0.2)
while True:
while ser.inWaiting():
# read a single character
char = ser.read()
# check we have the start of a LLAP message
if char == 'a':
# start building the full llap message by adding the 'a' we have
llapMsg = 'a'
# read in the next 11 characters form the serial buffer
# into the llap message
llapMsg += ser.read(11)
if "TMPB" in llapMsg:
TMPB = llapMsg[7:]
with open("TMPB.txt", "w") as text_file:
text_file.write(TMPB)
if float(TMPB) >= 19:
mail("temperature", "high", TMPB, "°C")
elif float(TMPB) <= 15:
mail("temperature", "low", TMPB, "°C")
else:
pass
elif "HUMB" in llapMsg:
HUMB = llapMsg[7:]
with open("HUMB.txt", "w") as text_file:
text_file.write(HUMB)
if float(HUMB) >= 80:
mail("humidity", "high", HUMB, "%")
elif float(HUMB) <= 70:
mail("humidity", "low", HUMB, "%")
else:
pass
elif "BATT" in llapMsg:
BATT = llapMsg[7:11]
with open("BATT.txt", "w") as text_file:
text_file.write(BATT)
if float(BATT) < 1:
mail("battery level", "low", BATT, "V")
else:
pass
sleep(0.2)
if __name__ == "__main__":
main()
I solved the question myself.
The while ser.inWaiting():
loop was causing the heavy cpu load.I removed it and corrected the indentation and it works great with a few % cpu load.
Thanks for your hints, it helped me solving the problem.
这篇关于Python脚本使用cpu过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!