Scheduling functions/commands in python

35 Views Asked by At

I am attempting a simple script that schedules a set of commands. I want to build, but I am stuck with the initial steps using the sched2 package

What I ultimately trying to do is to have a set of commands that will execute at a set of time delays. My failing attempt below:

from time import time
from datetime import datetime, timedelta
from sched2 import scheduler

sc = scheduler()
@sc.enter(delay=timedelta(minutes=1), priority=0)
print("this is a test at 1 min)

@sc.every(delay=timedelta(minutes=1.5), priority=0)
print("this is a test at 1.5 min")

sc.run()

Any assistant is appreciated.

1

There are 1 best solutions below

0
Josiah Witheford On

I don't have any experience with the sched2 library, but Python decorators are used to decorate functions, not just lines of code. Try wrapping your print statements inside a function.

from time import time
from datetime import datetime, timedelta
from sched2 import scheduler

sc = scheduler()

@sc.enter(delay=timedelta(minutes=1), priority=0)
def one_minute():
    print("this is a test at 1 min)

@sc.every(delay=timedelta(minutes=1.5), priority=0)
def one_and_half_minute():
    print("this is a test at 1.5 min")

sc.run()

This is what it looks like they are doing in the docs https://pypi.org/project/sched2/