循环程序-在其内部调用模块是一个坏主意吗?

发布于 2025-01-06 03:43:02 字数 784 浏览 0 评论 0原文

我有一个想要持续运行的程序。按照我现在的编码方式,一旦它到达其函数的末尾,它就会调用自身;它实际上从未到达其“End Sub”行。

Sub run

    *Run all code here*

    Call run

End Sub

人们对此提出了疑问,担心该程序永远没有机会“弹出”所有“推”到它上面的东西。基本上,一段时间后不断的自调用会使cpu过载。建议的替代方案是有一个永远不会结束的 while 循环,本质上

Sub run

While program_run = true then

    *Run all code here* 

Loop

End Sub

只是永远不会使 program_run = false。

  1. 我在“外行-y”中措辞了许多内容,因为我不确定正确的技术术语。

  2. 我熟悉列表和数据结构上下文中的术语“pop”和“push”,但仅限于基础水平;我认为这些具体术语不适用于这种情况,但我就是这么说的。 (该程序下载 .csv 并执行一系列数据操作,即格式化、复制/粘贴、上传到 MySQL 等)

  3. 这也与确保硬件不会稳定过载有关,因此如果这在超级用户中更合适,我理解。

问题:

  1. 对程序/硬件将“过载”的担忧是否合理?

  2. 自调用模块和永不结束的 while 循环之间有什么实际区别吗?

感谢您提前提供的任何帮助!

I have a program that I want to be continually running. The way I have it coded now, once it reaches the end of its' function, it calls itself; it never actually reaches its' End Sub line.

Sub run

    *Run all code here*

    Call run

End Sub

Questions have been raised about this, the concern being that the program never has a chance to "pop" everything that has been "pushed" onto it. Basically, after a certain period of time the constant self-calling would overload the cpu. The alternative that was suggested was instead to have a while loop that never ends, essentially

Sub run

While program_run = true then

    *Run all code here* 

Loop

End Sub

And just to never make program_run = false.

  1. I worded a number of things in "layman-y" because I'm not sure of the correct, technical terms.

  2. I'm familiar with the terms "pop" and "push" in the context of lists and data structures, but only on a basic level; I don't think that those specific terms apply to this situation, but that's how it was worded to me. (The program downloads .csv's and does a bunch of data manipulation, ie.formatting, copy/pasting, uploading to MySQL etc.)

  3. This also has to do with making sure that the hardware doesn't steadily get overloaded, so if this is more appropriate in SuperUser, I understand.

Questions:

  1. Is the concern that the program/hardware will be "overloaded" a legitimate one?

  2. Is there any practical difference between a self calling module and while loop that never ends?

Thank you for any help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2025-01-13 03:43:02

1. 对程序/硬件会“超载”的担忧是否合理?

取决于你所说的“超载”是什么意思。根据您的具体代码,这可能会导致一个处理器连续运行并因此升温。但计算机通常被设计为能够处理这个问题,只要不阻碍其通风,就不会造成问题。不过,它可能会很快耗尽笔记本电脑的电池。所有这些实际上取决于您的代码到底在做什么。

至于“程序重载”,我不知道这意味着什么,但它确实很好地引出了你的第二个问题:

2.自调用模块和永不结束的 while 循环之间有任何实际区别吗?

是的,有区别。既然您在 stackoverflow.com 上询问这个问题,您就会有兴趣知道您的第一段代码(自调用过程)实际上会导致 堆栈溢出。您没有“使 CPU 过载”,而是耗尽了调用堆栈上的可用内存。这个内存量不是无限的,但是每次您无限次调用 run 过程时,都会占用更多的内存,所以是的,在某些时候您会用完并得到一个堆栈溢出。

至于你的第二段代码,它不会编译。 (While ... then ... Loop 在 VBA 中不存在。您混合了三种不同类型的语法。)

如果您绝对确定希望程序运行到最后时间(就您的计算机而言),那么您可以像这样进行无限循环:

Sub run()
    Do
        '*Run all code here*
    Loop
End Sub

但是您不太可能真正希望代码运行到时间结束。我会问自己一个合理的最终条件是什么,然后实施它。

Sub run()
    Dim endConditionReached As Boolean
    endConditionReached = False
    Do While Not endConditionReached 
        '*Run all code here*
        ' code sets endConditionReached to True when appropriate
    Loop
End Sub

1. Is the concern that the program/hardware will be "overloaded" a legitimate one?

Depends what you mean by "overloaded". Depending on your exact code, this could cause one processor to run continuously and therefore heat up. But computers are usually designed to be able to deal with this, and it should not cause problems as long as you don't obstruct its ventilation. It could drain a laptop battery pretty quickly, though. All of this really depends on what exactly your code is doing.

As for "overloading the program", I don't know what this means, but it does lead nicely to your second question:

2. Is there any practical difference between a self calling module and while loop that never ends?

Yes, there is a difference. Since you are asking this on stackoverflow.com, you will be interested to know that your first bit of code (a self-calling procedure) will literally lead to a stack overflow. You're not "overloading the CPU", you are exhausting the memory available on the call stack. This amount of memory is not infinite, but a little bit more of it gets occupied each time you call your run procedure an infinite number of times, so yeah, at some point you run out and get a stack overflow.

As for your second bit of code, it will not compile. (While ... Then ... Loop does not exist in VBA. You're mixing up three different kinds of syntax.)

If you are absolutely sure that you want your program to run until the end of time (as far as your computer is concerned), then you can make an infinite loop like this:

Sub run()
    Do
        '*Run all code here*
    Loop
End Sub

However it is unlikely that you really, actually want your code to run until the end of time. I would ask myself what a plausible end condition would be, and implement it.

Sub run()
    Dim endConditionReached As Boolean
    endConditionReached = False
    Do While Not endConditionReached 
        '*Run all code here*
        ' code sets endConditionReached to True when appropriate
    Loop
End Sub
冷…雨湿花 2025-01-13 03:43:02

如果使用自调用函数,则每次调用自身时,都会将一个新函数添加到堆栈中。
一种简单的检查方法是在调试模式 (F8) 下运行下面的代码,并在几次调用后检查堆栈(查看/堆栈或 CTRL + L):您将看到堆栈有对该过程的多次调用。

Sub test()

  test

End Sub

如果您只是运行该代码(F5),您很快就会收到“堆栈空间不足”错误(在我的机器上一两秒后)。

如果您使用无限循环,假设您的循环没有任何内存泄漏,则不应出现任何溢出问题。但这不是很干净,因为您需要“杀死”该程序才能停止它。

另一种选择(取决于您想要实现的目标)是使用一种调度程序,该调度程序每 x 秒运行一次循环,并且可以根据需要停止。

If you use a self-calling function, everytime it calls itself, a new function is added to the stack.
An easy way to check that is to run the code below in debug mode (F8) and after a few calls, check the stack (View / Stack or CTRL + L): you will see that the stack has several calls to the procedure.

Sub test()

  test

End Sub

If you just run that code (F5), you will get an "out of stack space" error fairly quickly (after a second or 2 on my machine).

If you use an infinite loop, assuming your loop does not have any memory leaks, you should not have any overflow problems. But this is not very clean as you need to "kill" the procedure to stop it.

An alternative (depending on what you are trying to achieve) would be to have a sort of scheduler that runs your loop every x seconds and can be stopped on demand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文