简体   繁体   English

运行时间不定python脚本

[英]Running a python script for indefinite time

I have a python script that goes like this - 我有一个像这样的python脚本-

import..
def main ():
  some_condition check
  main() #calling main again
  some condition check
  main() #calling main again
main()

The idea here is to let the script run indefinitely and check for something. 这里的想法是让脚本无限期运行并检查某些内容。 This way of calling main() somehow seems incorrect. 这种以某种方式调用main()的方式似乎不正确。 I am quite new to Python scripting. 我对Python脚本非常陌生。 Can someone guide me if this very inefficient and if yes then how do I achieve this? 如果效率很低,有人可以指导我吗?如果可以,那么我该如何实现呢?

What you are doing is called recursion. 你在做什么是所谓的递归。 This is certainly not good for long running applications, since it would cause a stack overflow. 这对于长时间运行的应用程序当然不是很好,因为它会导致堆栈溢出。

Do your checking like this: 做这样的检查:

quit = False
while not quit:
   do_your_check()
   #maybe sleep
   quit = should_i_stop()

Just put the things you want to do in a while true loop. 只需将您想做的事情放入真实循环中即可。

import ...
def main():
  while True:
    some_condition check

Recursion is utilized when it is too complex/hard to write as iterative code. 当过于复杂/难以编写为迭代代码时,可以使用递归。 eg Tree traversals. 例如树遍历。

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

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