I am trying to have a function run once at a particular time. I have used My code is the following:
import pytz
import datetime
import time
def test_1():
print("Working_1")
def test_2():
print("Working_2")
while True:
current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
if current_time == '2019-03-18T19:00:36Z':
test_1()
current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
if current_time > '2019-03-18T19:00:36Z':
break
if current_time == '2019-03-18T19:00:36Z':
test_2()
current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
if current_time > '2019-03-18T19:00:36Z':
break
When I run my code it runs the first function followed by the second one until the end condition.
I want the function to run at the specified times in the if statement.
I think the problem is occurring about how loops are nested. I have tried multiple configurations of the if statement and break condition but just cannot get it.
Any help would be much appreciated, cheers. Sandy
ifstatements are comparing to the exact same time. From your post, I gather you intend things to happen at multiple different times. I'd start there.