简体   繁体   English

如何每分钟运行一次python脚本,但在某种情况下停止运行?

[英]How to run a python script once per minute, but stop upon a condition?

I have a python script that generates a random number and shoves it in variable "randomnumber". 我有一个python脚本,它会生成一个随机数并将其推入变量“ randomnumber”中。 If the randomnumber is equal to 100, then it stops. 如果随机数等于100,则停止。 Is there a way I could just do: 有什么办法可以做的:

python "randomnumber.py" and the contents execute every 10 seconds until I kill the script or if randomnumber is equal to 100 (will use sys.exit())? python“ randomnumber.py”,内容每10秒执行一次,直到我杀死脚本或randomnumber等于100(将使用sys.exit())?

This will run main every 10 seconds until it generates a 100 from a random number between 0 and 100 (inclusive). 直到它从0到100(含)之间的随机数生成100这将运行每10秒主。

import time, random

def main():
    #code goes here

while random.randint(0, 100) != 100:
    main()
    time.sleep(10)

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

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