如何永远运行脚本? [英] How to run a script forever?

查看:66
本文介绍了如何永远运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to run my Python program forever in an infinite loop..

目前,我正在这样运行它-

Currently I am running it like this -

#!/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 通话?有什么想法吗?

Is there any better way of doing it? Or do I even need time.sleep call? Any thoughts?

推荐答案

是的,您可以使用 while True:循环,该循环不会中断以连续运行Python代码.

Yes, you can use a while True: loop that never breaks to run Python code continually.

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

However, you will need to put the code you want to run continually inside the loop:

#!/usr/bin/python

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

此外, time.sleep 用于在一段时间内将脚本的操作暂停.因此,由于您希望自己的设备连续运行,所以我不知道为什么要使用它.

Also, time.sleep is used to suspend the operation of a script for a period of time. So, since you want yours to run continually, I don't see why you would use it.

这篇关于如何永远运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆