Python script running 'subprocess' using a lot of cpu on Rapsberry Pi Zero

124 Views Asked by At

I have a short python script that monitors the battery voltage of a solar folding project I'm running, and shuts off the Pi if the battery voltage drops below a certain threshold. The problem is that the script is using a constant 60-65% of the Pi Zero's only core. I also have Grafana and a small script that enters values into a database running on the Pi, but these are only sipping 2-5% sporadically. The system works fine, but I don't understand why the script with subprocess is using so much cpu. Here is the script:

#!/usr/bin/env python

import subprocess
from ina219 import INA219, DeviceRangeError
from time import sleep
import RPi.GPIO as GPIO

SHUNT_OHMS = 0.1
MAX_EXPECTED_AMPS = 3.19

ina40 = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS, address=0x40)
ina40.configure(ina40.RANGE_32V)

while True:
   v40 = ina40.voltage()
   print(v40)
   if v40 < 19:
      subprocess.call(["shutdown", "-h", "now"])
      sleep(5)
1

There are 1 best solutions below

4
cguk70 On

This is because you're asking the pi to keep checking the voltage as fast as it can so it will use as much CPU as it needs.

Just put a short sleep eg. sleep(0.1) at the end of the loop and it will cut down the CPU usage dramatically.