RSimple python schedule two time events

I am having problems with the following script using the python scheduling module. Basically, I want to fire the login event at time A, and then fire the action at time B.

The code does not run as the intended behavior is described 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 changed the code to the following, but it still seems to be wrong in terms of how best to implement this behavior.

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()

      

+3


source to share


2 answers


https://github.com/dbader/schedule

By following the pattern above, I was able to create the desired behavior 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)

      

+2


source


The following code has not been tested but should work. I put your original code in a comment so you can see where you are going wrong. You will probably need to refer to 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 latency. The correct function to use is scheduler.enterabs You will need a function to convert a date and time to a POSIX timestamp. It can be tricky in python 2.x due to timezone issue. Please note this question: Converting date and time to Unix timestamp and converting it back to 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()

      

+3


source







All Articles