0

i've read that it is a common issue, that some python scripts stops after about 3 hours. i have the same problem with mine. it is a script, that ligths up a LED strip like a fire. Unfortunately i'm really bad in python... do i have a memory issue after a while?

import RPi.GPIO as GPIO
import threading
import time
import random
import math

R = 17
G = 22

pwms = []
intensity = 1.0


def initialize_gpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup([17,22], GPIO.OUT)


def red_light():
    p = GPIO.PWM(R, 300)
    p.start(100)
    pwms.append(p)
    while True:
        p.ChangeDutyCycle(min(random.randint(50, 100) * math.pow(intensity + 0.1, 0.75), 100) if intensity > 0 else 0)
        rand_flicker_sleep()


def green_light():
    global green_dc
    p = GPIO.PWM(G, 300)
    p.start(0)
    pwms.append(p)
    while True:
        p.ChangeDutyCycle(random.randint(5, 10) * math.pow(intensity, 2) if intensity > 0 else 0)
        rand_flicker_sleep()


def rand_flicker_sleep():
    time.sleep(random.randint(3,10) / 100.0)



def fan_the_flame(_):
    global intensity
    intensity = min(intensity + 0.25, 1.0)


def light_candle():
    threads = [
        threading.Thread(target=red_light),
        threading.Thread(target=green_light),
##        threading.Thread(target=burning_down)
    ]
    for t in threads:
        t.daemon = True
        t.start()
    for t in threads:
        t.join()


def main():
    try:
        initialize_gpio()
        print("\nPress ^C (control-C) to exit the program.\n")
        light_candle()
    except KeyboardInterrupt:
        pass
    finally:
        for p in pwms:
            p.stop()

def stopfire():
        GPIO.cleanup()


if __name__ == '__main__':
    main()

5
  • 3
    This is a general (not specific to the Pi) programming question. I suggest you try to find out why the program seems to stop working by adding print statements and checking memory allocation.
    – joan
    Dec 6, 2021 at 8:34
  • How are you running this program?
    – CoderMike
    Dec 6, 2021 at 9:16
  • i'm starting it like this: python /home/pi/Desktop/fire.py it behaves the same, when i'm starting it with the same command as a cron job Dec 6, 2021 at 12:13
  • I would add a blank "except" clause, that just prints what the exception is. You are already checking for the one that you want to exit the program, so all other cases should get handled. My guess is that some random time, some of the calls in the thread fails, but rather then trying again, this causes the program to exit. Do you see any logging that indicates a failure when it exits?
    – Chad G
    Dec 6, 2021 at 21:48
  • 2
    If you just use 'python test.py' you may be using Python2 depending on your setup, better to use 'python3 test.py' which will ensure you are using Python3.
    – CoderMike
    Dec 8, 2021 at 16:45

1 Answer 1

0

thanks for you suggestions! funny story, as soon as i posted the question here, it never happened again! :D i'm stopping the running cron job by starting another one and killing all python processes. maybe this was the issue before i used cronjobs, that there were still some python processes in the background combined with a memory issues or something like that

Not the answer you're looking for? Browse other questions tagged or ask your own question.