1

So lets say i have this code:

...

connect()
find_links()
find_numbers()

in fact what it does is login to an account,get some numbers and one link: example:

1.23, 1.32 , 32.1, 2131.3 link.com/stats/

1.32, 1.41 , 3232.1, 21211.3 link.com/stats/

so all i want to do is make these functions run every one hour and then print the time so i can then compare results.I tried:

sched = BlockingScheduler()
@sched.scheduled_job('interval', seconds=3600 )

def do_that():
     connect()
     find_links()
     find_numbers()
     print(datetime.datetime.now())

but this just executes one time the functions and then just prints the date.

3
  • Did you try to lower the interval to something less that 3600 seconds in order to prove that it "executes only once"? Commented May 22, 2017 at 16:16
  • have u tried using python's sched.scheduler or threading.Timer ? Commented May 22, 2017 at 16:20
  • 1
    here is a better answer - stackoverflow.com/questions/373335/… Commented May 22, 2017 at 16:24

2 Answers 2

2

This should call the function once, then wait 3600 second(an hour), call function, wait, ect. Does not require anything outside of the standard library.

from time import sleep
from threading import Thread
from datetime import datetime


def func():
    connect()
    find_links()
    find_numbers()
    print(datetime.now())


if __name__ == '__main__':

    Thread(target = func).start()
    while True:
        sleep(3600)
        Thread(target = func).start()
Sign up to request clarification or add additional context in comments.

1 Comment

Why are you using threading here? Could you add some clarification please for us?
0

Your code may take some time to run. If you want to execute your function precisely an hour from the previous start time, try this:

from datetime import datetime
import time


def do_that():
    connect()
    find_links()
    find_numbers()
    print(datetime.now())


if __name__ == '__main__':

    starttime = time.time()
    while True:
        do_that()
        time.sleep(3600.0 - ((time.time() - starttime) % 3600.0))

Comments

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.