繁体   English   中英

定期调度 function 期间的递归错误

[英]Recursing Error during Periodic Sched function

我正在为 function 运行一些测试,我需要在其中编写两个单独的定时函数,每次执行的时间都是可变的。 这些功能必须是非阻塞的。

我创建的测试如下:

import sched, time
import threading

s = sched.scheduler(time.time, time.sleep)
s2 = sched.scheduler(time.time, time.sleep)

def do_something(sc):
    print("doing stuff.")
    sc.enter(5, 1, do_something, (sc,))
    sc.run()

def do_something_else(sc):
    print("doing other stuff.")
    sc.enter(3, 1, do_something_else, (sc,))
    sc.run()

x = threading.Thread(target=do_something, args=(s,), daemon=True)
x.start()
y = threading.Thread(target=do_something_else, args=(s,), daemon=True)
y.start()


while 1:
    pass

这个测试产生了我想要的结果。 在每种方法的每次调用中,我都可以更新下一次 function 调用的延迟量。

我计划使用它的程序将使用这种方法并使循环一次运行长达数小时。 我已经让这段代码运行了一段时间,看看它是否能够做到这一点。 大约 10-15 分钟我遇到了这个异常:

RecursionError: maximum recursion depth exceeded while calling a Python object

我是 python 的新手,来自 JS 和 Java,但这个错误是否类似于 stackoverflow? 这个错误是否会阻止我使用我的所有 memory 并使应用程序崩溃? 我看到使用系统模块可以改变深度。

据我了解,我创建的 sched 应该在下一个 sched 启动后终止并销毁,这意味着 memory 不能堆积? 如果我更改了递归限制,它是否允许我在 memory 崩溃的情况下运行我的程序所需的时间? 或者这个错误是否直接暗示我正在前往 memory 崩溃?

Python 设置了 1000 次迭代的标准递归限制。 但是您可以使用sys库对其进行修改。 您可以使用sys.getrecursionlimit() sys.setrecursionlimit()递归限制。

例如,如果要将递归限制更改为 2000,则可以运行sys.setrecursionlimit(2000)

但是,如果您将递归限制设置得太高,并且您的代码不断迭代,您可能会收到memory error并且您的程序会崩溃。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM