andrum99
Posts: 1239
Joined: Fri Jul 20, 2012 2:41 pm

hard hang reading CPU temperature from /sys

Thu Jan 22, 2015 8:51 pm

I have a python script that retrieves the Pi's CPU temperature from /sys/class/thermal/thermal_zone0/temp once a minute. I've seen a couple of hard hangs when reading this temperature. Here is an example from /var/log/messages:

Code: Select all

Jan 22 19:48:18 raspi2 temp_py2.py: Starting up
Jan 22 20:38:19 raspi2 temp_py2.py: Temperatures old#012Jan 22 20:38:19
Jan 22 20:39:44 raspi2 kernel: [ 3120.818067] python          D c044ab4c     0  2407      1 0x00000000
Jan 22 20:39:44 raspi2 kernel: [ 3120.818144] [<c044ab4c>] (__schedule+0x2a0/0x5ac) from [<c044903c>] (schedule_timeout+0x18c/0x21c)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818175] [<c044903c>] (schedule_timeout+0x18c/0x21c) from [<c044a574>] (__down+0x7c/0xb8)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818213] [<c044a574>] (__down+0x7c/0xb8) from [<c0040ea4>] (down+0x80/0x84)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818241] [<c0040ea4>] (down+0x80/0x84) from [<c001b4c0>] (dev_mbox_read+0x48/0x74)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818269] [<c001b4c0>] (dev_mbox_read+0x48/0x74) from [<c001b620>] (bcm_mailbox_property+0xa0/0x194)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818306] [<c001b620>] (bcm_mailbox_property+0xa0/0x194) from [<c033e548>] (bcm2835_get_temp_or_max.isra.0+0x50/0xc0)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818336] [<c033e548>] (bcm2835_get_temp_or_max.isra.0+0x50/0xc0) from [<c033be98>] (thermal_zone_get_temp+0x38/0x54)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818361] [<c033be98>] (thermal_zone_get_temp+0x38/0x54) from [<c033c010>] (temp_show+0x18/0x3c)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818405] [<c033c010>] (temp_show+0x18/0x3c) from [<c02aa728>] (dev_attr_show+0x1c/0x48)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818482] [<c02aa728>] (dev_attr_show+0x1c/0x48) from [<c014d110>] (sysfs_read_file+0x90/0x134)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818528] [<c014d110>] (sysfs_read_file+0x90/0x134) from [<c00e6bac>] (vfs_read+0x98/0x178)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818589] [<c00e6bac>] (vfs_read+0x98/0x178) from [<c00e7378>] (SyS_read+0x3c/0x78)
Jan 22 20:39:44 raspi2 kernel: [ 3120.818631] [<c00e7378>] (SyS_read+0x3c/0x78) from [<c000dec0>] (ret_fast_syscall+0x0/0x30)
When this hang occurs the python process cannot be killed, even using kill -9. Here is the relevant section of code from my script:

Code: Select all

file = open("/sys/class/thermal/thermal_zone0/temp", "r")
string = file.readline()
file.close()
Is this a known problem? Is there anything I can do about this?

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: hard hang reading CPU temperature from /sys

Fri Jan 23, 2015 12:15 am

Here's my version

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

while True:
  try:
    tFile = open('/sys/class/thermal/thermal_zone0/temp')
    temp = float(tFile.read())
    tempC = temp/1000
    if tempC > 43.5:
      GPIO.output(17, 1)
      print "HOT"
    else:
      GPIO.output(17, 0)
      print "COLD"

  except:
    tFile.close()
    GPIO.cleanup()
    exit
I don't close the file each time round the loop. The except clause is only to catch a [CTRL]+[C] to terminate the program.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

andrum99
Posts: 1239
Joined: Fri Jul 20, 2012 2:41 pm

Re: hard hang reading CPU temperature from /sys

Fri Jan 23, 2015 5:56 pm

That does seem more efficient. I put the code to read the temperature from /sys into a separate function to make the code a bit more readable - I don't really want to keep the file open between reads as that would complicate the code. Strangely, both times this problem occurred the script got the same process ID - it runs at startup from /etc/rc.local. I also had the whole Pi crash in the middle of the night last night - don't know why. Hopefully these are just glitches and there is no bug or anything like that. I think if there was someone would have come across it and it would probably have been fixed.

andrum99
Posts: 1239
Joined: Fri Jul 20, 2012 2:41 pm

Re: hard hang reading CPU temperature from /sys

Mon Jan 26, 2015 7:38 pm

I've tweaked my script to sleep for 0.1 seconds each time it goes round the button polling loop. So far it has not hung, so fingers crossed. :-)

Return to “Python”