简体   繁体   English

连续运行 Python 或限制 CPU 使用率的更好方法

[英]Better way to run Python continiously or limit CPU usage

I´m running a raspberry pi4 as an attendance tool.我正在运行一个树莓派 pi4 作为考勤工具。 The Pi itself is bored with the Tasks running on it which is basically a nodejs Server a Web Frontend aswell as a Postgres DB. Pi 本身对在其上运行的任务感到厌烦,它基本上是一个 nodejs 服务器、一个 Web 前端以及一个 Postgres DB。

However if I start my Python Script to check constantly for RFID Tags the CPU rises to 30% usage with one core maxed out which leads to a Temperature increase.但是,如果我启动我的 Python 脚本来不断检查 RFID 标签,CPU 的使用率会上升到 30%,其中一个核心会最大化,这会导致温度升高。

Since all other tasks are basically a no brainer for the PI I was wondering if there is any way to throttle this process down or a better approach for the python script.由于对于 PI 而言,所有其他任务基本上都是轻而易举的事情,我想知道是否有任何方法可以降低此过程,或者对于 python 脚本有更好的方法。

The script itself is absolutely basic but unfortunately needs to run in an endless loop like:该脚本本身是绝对基本的,但不幸的是需要在无限循环中运行,例如:

reader = SimpleMFRC522()
while true:
        id, text = reader.read()
        if(id):
              ...processData

I´m aware that 30% CPU usage is also pretty much nothing but since the PI does not have any FANs or Heatsinks on it I´ma little concerned about the Temperature in productive env.我知道 30% 的 CPU 使用率也几乎没有,但由于 PI 上没有任何风扇或散热器,我不太关心生产环境中的温度。

Also, since this task does basically nothing but check for rfid tags and if found post data to a restapi, I´m feeling like it´sa waste on used performance overall.此外,由于此任务基本上只检查 rfid 标签,如果发现将数据发布到 restapi,我觉得这对整体使用性能是一种浪费。

Thanks!谢谢!

You want your read call to block;你希望你的read调用被阻止; sadly, the very... thin "simple" wrapper you're using explicitly does a non-blocking read.可悲的是, 您正在使用的非常...薄的“简单”包装器显式执行非阻塞读取。 So, simply replace SimpleMFRC522 with an MFRC522 object and read blockingly.因此,只需将SimpleMFRC522替换为MFRC522 object 并进行阻塞读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM