schedule icon indicating copy to clipboard operation
schedule copied to clipboard

ret = job.run() ? what is the connection between ret and CancelJob?

Open yasinasama opened this issue 9 years ago • 3 comments

Hi! I just can not understand this method. def _run_job(self, job): ret = job.run() if isinstance(ret, CancelJob) or ret is CancelJob: self.cancel_job(job)

I think the ret and CancelJob is not related.ret just the return by my function. How to cancel job with your method.

yasinasama avatar Aug 02 '17 12:08 yasinasama

I am wondering the same thing. I've tried playing around with the code, but I can't seem to get that method to work. The only way I know how to cancel a job, at the moment, is by the following code snippet:

# Create two functions to schedule

>>> def func1():
           print('hello')

>>> def func2():
           print('goodbye')


# schedule those two functions and save the 'job' in variables, 'a' and 'b'
>>> a = schedule.every(5).seconds.do(func1)
>>> b = schedule.every(10).seconds.do(func2)

# print out the list of jobs scheduled
>>> schedule.default_scheduler.jobs
[Every 5 seconds do func1() (last run: [never], next run: 2017-08-07 22:28:50), Every 10 seconds do func2() (last run: [never], next run: 2017-08-07 22:29:18)]

# cancel job 'a'
>>> schedule.cancel_job(a)

# print out the list of job scheduled, note job 'a' has been removed
>>> schedule.default_scheduler.jobs
[Every 10 seconds do func2() (last run: [never], next run: 2017-08-07 22:31:03)]

I'm sure you already figured out that part. I don't know how the _run_job method works either, but I hope this helps.

trp07 avatar Aug 08 '17 02:08 trp07

Hey there, please check out the FAQ items related to job cancellation: https://schedule.readthedocs.io/en/stable/faq.html#how-can-i-run-a-job-only-once

Does this help?

dbader avatar Aug 08 '17 03:08 dbader

Thanks @dbader and @trp07 ! https://schedule.readthedocs.io/en/stable/faq.html#how-can-i-run-a-job-only-once It is useful!

yasinasama avatar Aug 08 '17 03:08 yasinasama