python - 如何永远运行脚本?

标签 python infinite-loop

我需要在无限循环中永远运行我的 Python 程序..

目前我是这样运行的 -

#!/usr/bin/python

import time

# some python code that I want 
# to keep on running


# Is this the right way to run the python program forever?
# And do I even need this time.sleep call?
while True:
    time.sleep(5)

有没有更好的方法呢?还是我什至需要 time.sleep 电话? 有什么想法吗?

最佳答案

是的,您可以使用永不中断的 while True: 循环来持续运行 Python 代码。

但是,您需要将要连续运行的代码放入循环中:

#!/usr/bin/python

while True:
    # some python code that I want 
    # to keep on running

另外,time.sleep用于暂停脚本的操作一段时间。所以,既然你想让你的持续运行,我不明白你为什么要使用它。

关于python - 如何永远运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20170251/

相关文章:

python - 需要建议在 python 中的 AWS API 中添加指数回退逻辑

python - “CsrfViewMiddleware”对象不可迭代

python - 在Python中使用lambda或helper方法来避免for循环重复

python - Flask 模型无法正确导入

c++ - For 循环中的 While 循环

c++ - 我的程序一直在循环,永远不会到达 "return 0;"。是编译器不好还是代码不好?

python - boolean 运算符如何在 'if' 条件下工作?

python - 使用 for 循环分配列表项会导致无限循环

c - OpenGL程序不会停止接受输入并且不显示输出

node.js - setTimeout - JavaScript 堆内存不足