Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite loop problem
#6
If you compare the old Library and the new from Circuit Python, you see that they raise a RuntimeError if something wrong happens.

https://github.com/adafruit/Adafruit_Cir...ht.py#L205
https://github.com/adafruit/Adafruit_Cir...ht.py#L209
https://github.com/adafruit/Adafruit_Cir...ht.py#L237
https://github.com/adafruit/Adafruit_Cir...ht.py#L241

The old library is also raises a RuntimeError: https://github.com/adafruit/Adafruit_Pyt...erry_Pi.py (just seek "raise ")

Corrected version, but not tested.

import logging
import sys
import time

import Adafruit_DHT as adht

logging.basicConfig(
    filename="temperature.log",
    filemode="a",
    format="%(created)f %(message)s",
    level=logging.INFO,
)
log = logging.getLogger()


def main_loop():
    while True:
        try:
            humidity, temperature = adht.read_retry(adht.DHT22, 23)
        except RuntimeError as e:
            # this block, if RuntimeError were raised from adht.read_retry
            log.error(f"RuntimeError: {e}")
        else:
            # this block, if no Exception were raised by adht.read_retry
            log.info(f"Temp={temperature:0.1f} C and Humidity={humidity:0.1f} %")
        # this is always executed
        print("Waiting for 60 seconds… Sensor 0", file=sys.stderr)
        time.sleep(60)


if __name__ == "__main__":
    try:
        main_loop()
    except KeyboardInterrupt:
        print("\nCTRL+C\n", file=sys.stderr)
By the way, I use always black and isort for code formatting and sorting of imports.

For example the module from Adafruit is a third-party module. This should be imported after first party packages like sys, os, etc.

Other changes:
  • f-strings
  • log = logging.getLogger() and use this instead of logging.error, logging.info
  • printing to sys.stderr
  • loop is in a function
  • try-except around main_loop
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Infinite loop problem - by Zirconyl - Nov-15-2020, 04:50 PM
RE: Infinite loop problem - by Aspire2Inspire - Nov-15-2020, 05:16 PM
RE: Infinite loop problem - by Zirconyl - Nov-15-2020, 05:59 PM
RE: Infinite loop problem - by perfringo - Nov-15-2020, 08:27 PM
RE: Infinite loop problem - by Zirconyl - Nov-15-2020, 09:02 PM
RE: Infinite loop problem - by DeaD_EyE - Nov-16-2020, 09:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 195 May-18-2024, 03:07 PM
Last Post: jasserin
  While Loop Problem Benno2805 1 637 Sep-06-2023, 04:51 PM
Last Post: deanhystad
Shocked Why this code does not go into an infinite loop? 2367409125 2 955 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,276 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,634 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  using 'while loop' output going into infinite loop... amitkb 2 2,055 Oct-05-2020, 09:18 PM
Last Post: micseydel
  single input infinite output problem Chase91 2 2,041 Sep-23-2020, 10:01 PM
Last Post: Chase91
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,240 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Infinite loop not working pmp2 2 1,720 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Python loop problem Kristenl2784 11 5,298 Jun-18-2020, 07:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020