python cpu占用高,Python脚本占用cpu太多

我不是编程专家,所以我在google上搜索了很多东西来让这个脚本工作。它监听串行接口,并搜索3个值(温度、湿度和电池电量)。如果找到其中一个zhem,它会将其保存到一个文本文件中,并检查该值是否高于或低于某个级别。如果是这种情况,它会发送电子邮件警告。在

我的问题是它持续消耗99%的cpu功率。。。

你能帮我把CPU的使用限制在最低限度吗。在

谢谢#!/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 = 'sender@domain.com'

toaddrs = 'recipient@domain.com'

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值