Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run job once and only once #33

Closed
Woodamsc opened this issue Jan 17, 2015 · 3 comments
Closed

Run job once and only once #33

Woodamsc opened this issue Jan 17, 2015 · 3 comments

Comments

@Woodamsc
Copy link

In my uses I've been wanting to run a job only once at a specific time, then cancel it.
This program is so intuitively worded that I keep thinking it's supported.
Something like schedule.today.at('10:30').do(job)

I've been working around it by scheduling it every hour/day/week at the time I desire, then cancelling the job right after it executes. It's a tad tedious though because I have to manually keep a reference to the job ID to later cancel that one job, and it looks clunky of course.

@dbader
Copy link
Owner

dbader commented Feb 12, 2015

Hi @ydoow,

you can achieve that by doing the following:

def job_that_executes_once():
    # Do some work ...
    return schedule.CancelJob

schedule.today.at('10:30').do(job_that_executes_once)

Basically you let the job cancel itself the first time it executes.

@dbader
Copy link
Owner

dbader commented Feb 12, 2015

Added this to the FAQ in #35.

@Trever123
Copy link

Sorry to comment on an old issue but the above code snippet does not actually seem to work. I have just tried the following and got the error that the attribute today does not exist (i also checked the code and can't see today as part of scheduler or job):

>>> import schedule
>>> def job():
...     print "its time"
...     return schedule.CancelJob
... 
>>> schedule.today.at('22:30').do(job)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'today'

Then to double check that scheduler is working for me i did the following:

>>> schedule.every().day.at('22:30').do(job)
Every 1 day at 22:30:00 do job() (last run: [never], next run: 2015-07-13 22:30:00)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants