0

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

9
  • 1
    why is your code double-spaced..? Commented Mar 18, 2019 at 19:16
  • I just find it a bit easier to read. Commented Mar 18, 2019 at 19:17
  • What is the problem with your code? How did it not meet expectations? Commented Mar 18, 2019 at 19:17
  • I would like the functions to run at the times specified in the if statement. This should be changed to sompoint in the future. Commented Mar 18, 2019 at 19:18
  • All four of your if statements 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. Commented Mar 18, 2019 at 19:19

2 Answers 2

2

I believe your code here doesn't work because it would require the time to be EXACTLY the timestamp given, and when accounting for run-time, even if minimal, this is quite unrealistic. A possible solution is to give a "time cushion", i.e a window around the execution time rather than an exact match for the if statement; however this is still quite unreliable.

If you don't mind using an external module, I would recommend you take a look at sched or APScheduler. I've personally used the latter, and it works great.

Sign up to request clarification or add additional context in comments.

Comments

1

Taking my best guess at what you want: You want both functions to run, in order, at a particular time, yes? Something much simpler should work, like this:

current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
while current_time < '2019-03-18T19:00:36Z':
    current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
test_1()
test_2()

This will run both functions as soon as the clock hits or passes your target time.

EDIT: If you want them to run at two different times:

current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
while current_time < '<first_time>':
    current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
test_1()
while current_time < '<second_time>':
    current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%SZ')
test_2()

(where <first_time> is first chronologically)

2 Comments

The current_time can be compared '2019-03-18T18:35:36Z' > '2019-03-18T18:34:36Z' returns true
What if I want the functions to be run at a particular time. Maybe this code will help you understand my question def time(): current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M') while current_time == '2019-03-18T19:37': current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M') test_1() while current_time == '2019-03-18T19:38': current_time = datetime.datetime.now(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M') test_2()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.