3
votes

I am having trouble with the following scenario using python schedule module. Essentially I want to run a login event at time A, and then run the action at time B.

The code does not run as the intended behaviour describes and this is where I need help.

import sched
import datetime

today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)

scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)

def login_event(name):
    print 'EVENT:', datetime.datetime.today(), name

def action_event(name):
    print 'EVENT:' datetime.datetime.today(),name

print 'START:', time.time()
scheduler.enter(log, login_event, ('Login'))
scheduler.enter(action, login_event, ('Action'))

scheduler.run()

EDIT I have altered the code to the following but it still doesn't seem right in terms of how best to implement this behaviour.

import sched
import datetime
from datetime import timedelta
import datetime
import time

today = datetime.datetime.today()
log = datetime.datetime.now() + timedelta(minutes=1)# today.replace(hour=12, minute=46, second = 0)
action= log + timedelta(minutes=2)


scheduler = sched.scheduler(time.time, time.sleep)
print datetime.datetime.now

def login_event(name):
    print 'Login:', datetime.datetime.now(), name

def action_event(name):
    print 'Action:', datetime.datetime.now(), name

print 'Start:', datetime.datetime.now()

scheduler.enter(1, 1, login_event, ('first',))
scheduler.enter(60, 1, action_event, ('second',))

scheduler.run()
2
So, what's your problem? - Haochen Wu
The code above does not work as the intended behaviour describes. I need help adjusting the scheduler code to work in the desired manner - Mdev
Can you provide more specific description of how it fails cause I can see more than one place that may need modification in your code. - Haochen Wu
the schedular can not be used this way, it's all being used wrong? - Mdev

2 Answers

3
votes

The following code hasn't been tested but should be work. I've put your original code into comment so you can see where you got wrong. You will probably need to refer the doc: https://docs.python.org/2/library/sched.html

import sched, time
import datetime

today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)

#scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
#The first argument of sched.scheduler should be a function that return a number.
scheduler = sched.scheduler(time.time, time.sleep)

def login_event(name):
    print 'EVENT:', datetime.datetime.today(), name

def action_event(name):
    print 'EVENT:', datetime.datetime.today(),name

print 'START:', time.time()

scheduler.enter is used for relative delay. The correct function to use is scheduler.enterabs You will need a function to convert datetime to POSIX timestamp. This can be tricky in python 2.x due to timezone issue. Refer to this question: Convert datetime to Unix timestamp and convert it back in python Also, the function takes 4 arguments.

#scheduler.enter(log, login_event, ('Login'))
#scheduler.enter(action, login_event, ('Action'))
scheduler.enterabs(timestamp(log), 0, login_event, ('Login'))
scheduler.enterabs(timestamp(action), 0, action_event, ('Action'))

scheduler.run()
2
votes

https://github.com/dbader/schedule

By following the pattern linked above I was able to create the desired behaviour using a slightly different schedule module

import schedule
import time

def job():
    print("I'm working on job one...")

def job2():
    print("I'm working on job two..")

schedule.every().day.at("10:30").do(job)
schedule.every().day.at("10:35").do(job2)


while True:
    schedule.run_pending()
    time.sleep(1)