简体   繁体   English

Python RecursionError:超出最大递归深度

[英]Python RecursionError: maximum recursion depth exceeded

I have a python program I wrote that moves my solar panel with the sun throughout the day.我有一个我写的 python 程序,它可以全天随着太阳移动我的太阳能电池板。 It works fine, but about twice a week I get RecursionError: maximum recursion depth exceeded它工作正常,但大约每周两次我收到 RecursionError: maximum recursion depth exceeded

I'm pretty new to python and I've looked up Recursion Errors, it says when a function calls itself too many times, I don't see where that is happening in this program.我对 python 很陌生,我查过递归错误,它说当一个函数调用自己太多次时,我看不到这个程序中发生了什么。

    from datetime import date
    from datetime import datetime, timezone, timedelta
    import astral
    from astral import Astral
    import time
    import pytz


    # import RPi.GPIO as GPIO

    # Global Variables
    ast = Astral()
    city_Name = 'Cleveland'
    local_City = ast[city_Name]
    # sun_Position = local_City.sun(local=True)
    reset_Solar = True

    # Retrieves and returns current time
    def get_Current_Time():
        eastern = pytz.timezone('America/New_York')
        curr_Time = datetime.now(eastern)
        return curr_Time

    def main_Function():
        global local_City
        sun_Position = local_City.sun(local=True)
        current_Time = get_Current_Time()
        solar_Sunrise = sun_Position.get('sunrise')
        solar_Noon = sun_Position.get('noon')
        solar_Sunset = sun_Position.get('sunset')
        calc_Sunrise_Noon = solar_Noon - solar_Sunrise

        total_Seconds = calc_Sunrise_Noon.seconds
        calc_Hours, remainder = divmod(total_Seconds, 3600)
        calc_Minutes, calc_Seconds = divmod(remainder, 60)

        time_To_Adjust = total_Seconds / 24

        print (current_Time)
        print (solar_Sunrise)

        if current_Time >= solar_Sunrise and current_Time < solar_Sunset:
            solar_Adjust_Active(time_To_Adjust)
        elif reset_Solar == True:
            reset_Solar_Panel()
        else:
            solar_Adjust_Deactive()

    def solar_Adjust_Active(time_To_Adjust):

        while time_To_Adjust > 0:
            current_Time = get_Current_Time()
            time_To_Adjust = time_To_Adjust - 1
            time.sleep(1)
            print (current_Time)
            print (time_To_Adjust)
        daylight_Adjustment()

    def solar_Adjust_Deactive():
        global local_City
        curr_Time = get_Current_Time()
        calc_Tomorrow = curr_Time.date() + timedelta(days=1)
        sun_Position_Tomorrow = local_City.sun(local=True, date = calc_Tomorrow)
        solar_Sunrise_Tomorrow = sun_Position_Tomorrow.get('sunrise')

        time_Till_Sunrise = solar_Sunrise_Tomorrow - curr_Time

        sunrise_Total_Seconds = time_Till_Sunrise.seconds
        calc_Sunrise_Hours, remainder = divmod(sunrise_Total_Seconds, 3600)
        calc_Sunrise_Minutes, calc_Sunrise_Seconds = divmod(remainder, 60)

        while sunrise_Total_Seconds > 0:
            sunrise_Total_Seconds = sunrise_Total_Seconds - 1
            time.sleep(1)
            # print ('Seconds till Sunrise', sunrise_Total_Seconds)
        print (solar_Sunrise_Tomorrow)
        main_Function()

    def daylight_Adjustment():
        global reset_Solar
        # Adustment to Solar Panel
        #GPIO.setmode(GPIO.BCM)

        # init pin numbers
        #pin_Open = [6]

        # set mode default state is 'low'
        #GPIO.setup(pin_Open, GPIO.OUT) 

        # Activate Open Relay to High (High turns Relay on)
        #GPIO.output(pin_Open, GPIO.HIGH)     # Activate Open relay

        # Start Timer for duration actuator will be activated
        timer = 0
        while timer < 1:
            time.sleep(1)
            timer = timer + 1

        print ('Panal adjusted')
        # set Open relay back to low (Turns Relay off)
        #GPIO.output(pin_Open, GPIO.LOW)

        # Reset GPIO settings
        #GPIO.cleanup()

        reset_Solar = True
        main_Function()

     def reset_Solar_Panel():
        global reset_Solar
        print ('Setting panel back to original position')
        # Adustment to Solar Panel
        # GPIO.setmode(GPIO.BCM)

        # init pin numbers
        # pin_Open = [XX]

        # set mode default state is 'low'
        # GPIO.setup(pin_Open, GPIO.OUT) 

        # Activate Open Relay to High (High turns Relay on)
        # GPIO.output(pin_Open, GPIO.HIGH)     # Activate Open relay

        # Start Timer for duration actuator will be activated
        timer = 0
        while timer <= 48:
            time.sleep(1)
            timer = timer + 1

        # set Open relay back to low (Turns Relay off)
        # GPIO.output(pin_Open, GPIO.LOW)

        # Reset GPIO settings
        # GPIO.cleanup()

        reset_Solar = False
        main_Function()

    main_Function()

You have (at least one) a loop in your code:您的代码中有(至少一个)循环:

def main_Function(): 
->  solar_Adjust_Active(time_To_Adjust)  
->  daylight_Adjustment() 
->  main_Function() -> .... # lets run that for a couple of days ...

Normally you have a main loop (fe while True: ) and call/return from functions into your main loop without recursing:通常你有一个主循环(fe while True: )并且从函数调用/返回到你的主循环而不递归:

Pseudocode:伪代码:

def resetToStart():
    move panels to sunrise position
    return

def movePanel():
    get_currnt_optimal_position_for_datetime
    move_to_that_position
    return

resetToStart()  # start at a well known position
while True:
    if NightTime:  # sun is down
        resetToStart() 
        sleep till daytime
    elif DayTime:  # sun is up
        movePanel() 
        sleep some till adjustment needed

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecursionError:在比较Python中超出了最大递归深度 - RecursionError: maximum recursion depth exceeded in comparison Python Python Tkinter:递归错误:超出最大递归深度 - Python Tkinter: RecursionError: maximum recursion depth exceeded Python - RecursionError - 超过最大递归深度 - Python - RecursionError - maximum recursion depth exceeded RecursionError:超过最大递归深度 - RecursionError: maximum recursion depth exceeded python迷宫递归问题,RecursionError:比较中超出最大递归深度 - python maze recursion problem, RecursionError: maximum recursion depth exceeded in comparison Python:RecursionError:调用 Python 对象时超出了最大递归深度 - Python: RecursionError: maximum recursion depth exceeded while calling a Python object Python RecursionError:调用Python对象时超出了最大递归深度 - Python RecursionError: maximum recursion depth exceeded while calling a Python object 如何使用 Python 中的 Eventlet 和 Requests 解决“RecursionError:超出最大递归深度” - How to solve 'RecursionError: maximum recursion depth exceeded' with Eventlet and Requests in Python RecursionError at : 调用 Python 对象时超出了最大递归深度 - RecursionError at : maximum recursion depth exceeded while calling a Python object Django - RecursionError: 调用 Python object 时超出最大递归深度 - Django - RecursionError: maximum recursion depth exceeded while calling a Python object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM