If Time Exceeds X Minutes
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 woul
Solution 1:
from time import perf_counter
whileTrue:
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
Solution 2:
Here is code which prints "BUY" every time the price is above the threshold, and sends a notification if no notifications have been sent in the previous 60 seconds.
import time
import random
defget_price():
return random.random()
buy = 0.2# Threshold price for buying
notification_time = 0# This initial value ensures that the first notification is sent.whileTrue:
if get_price() <= buy:
print ("BUY!")
if time.time()-notification_time > 60:
notification_time = time.time()
print("Sending notification")
time.sleep(1) # Wait 1 second before starting the next loop
Especially in python, you want to avoid doing stuff manually, like you do by taking the tm_min from the time object. You can usually get better results by using the existing libraries, such as subtraction between two timestamps.
Post a Comment for "If Time Exceeds X Minutes"