Python Forum
Cancelling previous schedule
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cancelling previous schedule
#1
Hi,

Just starting out with Python and am using VSCode to schedule a simple task so am using the following to get started:
import sched
import time

s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    print("Doing stuff...")
    # do your stuff
    s.enter(2, 1, do_something, (sc,))

s.enter(2, 1, do_something, (s,))
s.run()
This runs ok and I get "Doing stuff" printed in the terminal but I was struggling to see what the units were - I can see that it was run after 2 somethings (seconds I believe) but I couldn't see from any documentation I could find how you might change this to days or hours etc, so I went looking for some other code and came up across this:
import schedule
import time

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

schedule.canceljob
schedule.every(10).seconds.do(job)

while 1:
   schedule.run_pending()
   time.sleep(1)
Again this looks like what I need but I cannot run as whenever I do, I get "Doing stuff" printed in the terminal, instead of "I'm working..." which is what I'd expect and leaves me to believe there is something of the previous code still running despite cancelling with ctrl-c. Both of these code examples are in separate files and the one which isn't being worked on is closed.

Is there something going on in the background with schedule which is not being closed down? If so, how can you close off previous jobs when the code exits???

Thanks
Reply
#2
(Sep-14-2021, 07:48 PM)cosmarchy Wrote: Is there something going on in the background with schedule which is not being closed down?
Yes you have to use Ctrl+c to shut it down.
Your second code with schedule is line 7 wrong.
Code will not run with that error,look at Cancel a job doc
So a example run.
import schedule
import time

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

schedule.every(10).seconds.do(job)
while 1:
   schedule.run_pending()
   time.sleep(1)
Output:
G:\div_code\answer λ python 10_sec.py I'm working... I'm working... I'm working... I'm working... Traceback (most recent call last): File "G:\div_code\answer\10_sec.py", line 10, in <module> time.sleep(1) KeyboardInterrupt ^C
In background there will a process python.exe(python on Linux) running which has a own pid.
If want to see processes in background eg shut down from Python look at psutil.
Reply
#3
Here's something I wrote back in 2016 which may be of interest: https://python-forum.io/thread-1304.html
a timer can be set to wait days, hours, minutes, and/or seconds, for simultaneous projects.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hosting a script on a VPS, and on a schedule? oldguy 0 2,963 Mar-13-2021, 02:46 AM
Last Post: oldguy
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,192 May-08-2020, 03:14 PM
Last Post: DPaul
  something i noticed about the "schedule" module Stan2292 1 1,777 Aug-30-2019, 06:04 AM
Last Post: buran
  How to schedule Jupyter Notebooks with Papermill wendysling 0 2,521 Jun-11-2019, 05:53 PM
Last Post: wendysling
  schedule module conundrum wgovideo 11 4,361 May-29-2019, 06:39 PM
Last Post: wgovideo
  I got a problem with python schedule darktitan 2 3,513 Sep-22-2018, 12:49 PM
Last Post: darktitan
  Schedule with other processing RValentim 7 3,995 Jul-10-2018, 12:57 PM
Last Post: RValentim
  How to convert Schedule to APScheduler module? penguin9 2 3,781 May-03-2018, 12:44 PM
Last Post: penguin9
  Creating a schedule program ndplokkaar 4 3,981 Nov-23-2017, 04:21 PM
Last Post: ndplokkaar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020