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.

Community
  • 1
  • 1
aleale
  • 1,231
  • 2
  • 8
  • 11
  • Did you try to lower the interval to something less that 3600 seconds in order to prove that it "executes only once"? – errata May 22 '17 at 16:16
  • have u tried using python's sched.scheduler or threading.Timer ? – aksss May 22 '17 at 16:20
  • 1
    here is a better answer - https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python – aksss May 22 '17 at 16:24

2 Answers2

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()
RandomHash
  • 669
  • 6
  • 20
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))
bnye
  • 41
  • 5