如果时间超过 x 分钟 [英] if time exceeds x minutes

查看:36
本文介绍了如果时间超过 x 分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,并且有一个从 json 中提取数据的循环.我已经用 if 和 else 语句设置了变量,所以当变量匹配时,它会发送一个通知.但是,我想在 if 中添加另一个 if 语句,以便在窗口中显示数据,但另一个如果时间超过 5 分钟,则发送通知.

im new to python and have a loop that pulls data from a json. i have set variables with if and else statements so when the variables match, it sends a notification. however, i would like add another if statement within the if so it displays the data in the window but the other if time exceed 5 minutes, send notification.

我试图设置类似

    import time
    time = time.gmtime().tm_min
    while True:
       if price <= buy:
          print ("BUY!")
          if time > timeout:
             send message code
             timeout = time.gmtime().tm_min + 1
                if timeout > 59:
                timeout = 00

由于脚本循环运行,我认为时间必须不断更新.一旦 if 语句被触发,发送一条消息并将 5 分钟添加到时间变量,以便在下一个循环中,如果语句为真,如果时间未超过 5 分钟,则不要运行.我说 5 分钟,但在实际代码中我有 1 分钟,原因有两个.第一个原因是因为第一个 if 语句不会持续那么长时间.大约 30 分钟后,它离价格更远了.第二个原因是因为我不知道如何在 59 时间后用 python 环绕:P.

since the script is running on a loop, i think the time has to keep updating. once the if statements are triggered, send a message and add 5 minutes to the time variable so that on the next loop if the statement is true, dont run if the time hasnt exceeded 5 minutes. i say 5 minutes but in the actual code i have 1 minute for 2 reasons. the first reason is because the first if statement doesnt last that long. after about 30minutes it goes further away from the price. the 2nd reason is because i dont know how to wrap around in python after 59 in time :P.

推荐答案

from time import perf_counter

while True:
    start_time = perf_counter() + 300  # Set limit to 5 minutes (60 seconds * 5)

    if price <= buy:
        print ("BUY!")

    if perf_counter() > start_time:
        #send message code

这篇关于如果时间超过 x 分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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