Python 递归限制

1 投票
3 回答
6848 浏览
提问于 2025-04-16 16:16

我明白为什么递归的限制是1000。我的意思是,我想让一个脚本持续运行,但我理解的对吗?最终递归的限制会被达到(即使我把它设置得更高),然后Python会崩溃吗?

从整体来看,这其实没什么大不了的,因为我可以让操作系统不断重启这个脚本,但我在想,是否有更优雅的解决办法可以在脚本内部实现(比如换线程?)。

我的脚本:

import os
import subprocess
import time
import logging
import datetime
from sys import argv

if len(argv) < 3:
    exit('Please provide two arguments - Source Destination')


LOC_DIR = argv[1]
REM_DIR = argv[2]

POLL_INT = 10
RUN_INT = 60
FILE_EXT = '.mov'


# logging setup
logging.basicConfig(filename='%s' % os.path.join(LOC_DIR, '%s the_log.log' % datetime.datetime.now()),level=logging.DEBUG)

# make an easy print and logging function
def printLog(string):
    print '%s %s' % (datetime.datetime.now(), string)
    logging.info('%s %s' % (datetime.datetime.now(), string))


# get the files with absolute paths
def getFiles(path):
    return [os.path.join(path, entry) for entry in os.listdir(path)]


# check if file is still being copied (file size has changed within the poll interval)
def checkSize(path):
    same = False
    while same is False:
        printLog("Processing '%s'" % os.path.basename(path))
        printLog('Waiting %s seconds for any filesize change' % POLL_INT)
        size1 = os.path.getsize(path)
        time.sleep(POLL_INT)
        size2 = os.path.getsize(path)
        if size1 == size2:
            same = True
            printLog('File size stayed the same for %s seconds' % POLL_INT)
            return same
        else:
            printLog('File size change detected. Waiting a further %s seconds' % POLL_INT)


# check if correct file extension
def checkExt(path):
    if path.endswith(FILE_EXT):
        return True


# rsync subprocess
def rsyncFile(path):
    printLog("Syncing file '%s'" % os.path.basename(path))
    try:
        command = ['rsync', '-a', '--remove-source-files', path, REM_DIR]
        p = subprocess.Popen(command, stdout=subprocess.PIPE)
        for line in p.stdout:
            printLog("rsync: '%s'" %line)
        p.wait()
        if p.returncode == 0:
            printLog('<<< File synced successfully :) >>>')
        elif p.returncode == 10:
            printLog('****** Please check your internet connection!! ******  Rsync error code: %s' % p.returncode)
        else:
            printLog('There was a problem. Error code: %s' % p.returncode)
    except Exception as e:
        logging.debug(e)


# main logic
def main():
    all_files = getFiles(LOC_DIR)
    files = []
    for f in all_files:
        if checkExt(f):
            files.append(f)
    if len(files) == 1:
        printLog('<<< Found %s matching file >>>' % len(files))
    elif len(files) > 1:
        printLog('<<< Found %s matching files >>>' % len(files))
    for f in files:
        if checkSize(f):
            rsyncFile(f)
    printLog('No files found.  Checking again in %s seconds' % RUN_INT)
    time.sleep(RUN_INT)
    printLog('Checking for files')
    main()

if __name__ == "__main__":


    main()

3 个回答

0

根据我的理解,递归限制只是在使用递归函数时才会设置,所以如果你真的想要重复执行某个操作,可以直接运行。

while True:
   #repeated stuff goes here

递归是一个很棒的工具,但要小心使用,它有时会让你陷入困境。你说得对,Python的递归调用最多只能进行1000次,如果你的递归方法在这之前没有完成,就会出现错误。

祝你好运。

1

你这样做是不对的。

把主循环换成一个循环。

# main logic
def main():
    while True:
        all_files = getFiles(LOC_DIR)
        files = []
        for f in all_files:
            if checkExt(f):
                files.append(f)
        if len(files) == 1:
            printLog('<<< Found %s matching file >>>' % len(files))
        elif len(files) > 1:
            printLog('<<< Found %s matching files >>>' % len(files))
        for f in files:
            if checkSize(f):
                rsyncFile(f)
        printLog('No files found.  Checking again in %s seconds' % RUN_INT)
        time.sleep(RUN_INT)
        printLog('Checking for files')
7

CPython对递归没有优化,所以你最好避免使用深度递归的代码,改用普通的循环来实现:

def main():
    while True:
        all_files = getFiles(LOC_DIR)
        files = []
        for f in all_files:
            if checkExt(f):
                files.append(f)
        if len(files) == 1:
            printLog('<<< Found %s matching file >>>' % len(files))
        elif len(files) > 1:
            printLog('<<< Found %s matching files >>>' % len(files))
        for f in files:
            if checkSize(f):
                rsyncFile(f)
        printLog('No files found.  Checking again in %s seconds' % RUN_INT)
        time.sleep(RUN_INT)
        printLog('Checking for files')

if __name__ == "__main__":    
    main()

撰写回答