adafruit / Adafruit_Python_PlatformDetect

MIT License
59 stars 238 forks source link

Too many open files #134

Closed brettrussellza closed 3 years ago

brettrussellza commented 3 years ago

I am getting the following error after reading the MCP3008 sensor over SPI successfully for about 4 hours:

Traceback (most recent call last): File "piSolarMonitor.py", line 438, in MainTimer File "piSolarMonitor.py", line 350, in ReadPin File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp3xxx/analog_in.py", line 70, in value File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp3xxx/mcp3xxx.py", line 97, in read File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/spi_device.py", line 93, in __enter__ File "/usr/local/lib/python3.7/dist-packages/busio.py", line 198, in configure File "/usr/local/lib/python3.7/dist-packages/adafruit_platformdetect/board.py", line 383, in any_raspberry_pi File "/usr/local/lib/python3.7/dist-packages/adafruit_platformdetect/board.py", line 188, in _pi_rev_code File "/usr/local/lib/python3.7/dist-packages/adafruit_platformdetect/__init__.py", line 49, in get_cpuinfo_field OSError: [Errno 24] Too many open files: '/proc/cpuinfo'

Here is my code for reading the sensor, runs every 10 seconds on a timer:

` def ReadPin(self): with busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) as spi: cs = digitalio.DigitalInOut(self.CS.value) mcp = MCP.MCP3008(spi, cs) chan = AnalogIn(mcp, self.Pin) self.RawADC = (chan.value/64) + self.ToleranceAdjust

    self.Voltage = round(VoltageDiv(self.RawADC, self.Resistor1, self.Resistor2) + self.ToleranceAdjust,2)
    log.debug('CS: ' + str(self.CS) + ' Pin: ' + str(self.Pin) + ' Name: ' + self.Name + ' Voltage (ADC): ' + str(self.Voltage) + '(' + str(self.RawADC) + ')')
    log.info('Name: ' + self.Name + ' Voltage (ADC): ' + str(self.Voltage))`

Any chance you can investigate this please. Running on a Rapberry PI 3B.

ladyada commented 3 years ago

please create a minimal FULL example python script running on the latest raspberry pi OS with all libraries updated

makermelissa commented 3 years ago

Ok, I think this is caused by 2 issues. First, platformdetect is not closing files after detecting the board. Under normal circumstances, this is not an issue. However, the other part contributing to this is it appears you are calling busio.SPI inside of a function, so it runs the platformdetect code every time it accesses that. Ideally, you would assign that to a variable once and either pass that into your function or access it globally. I have PR #135 in to address the files not being closed automatically.