(SOLVED) APScheduler info and how to terminate a running way

This is the place for queries that don't fit in any of the other categories.

(SOLVED) APScheduler info and how to terminate a running way

Postby Grinch » Thu Oct 03, 2013 7:40 am

Dear All,
I'm trying to use this class to add some scheduling functionality to my application.
At now I'm working around a script configurator utility, see the attached image with name image1.jpg.
With the aid of the script configurator I should be able to choose the day of the week, the month and the starting time in a set spanning from 00 to 23 (24h format) with the freedom to choose the starting minutes that will be the same for every hour selected (if is possible select also a custom minute for every hour this should be the best choice but at this time I don't know if this is possible).
Suppose that you like to start a job every January and February on Monday and Thursday at this time 00:30, 12:30, and 23:30 In my thinking I've to set the form as shown in image2.jpg.
Now I like to know how to compose the command string that I've to pass to the scheduler in order to setup this job.

From the web page http://pythonhosted.org/APScheduler/cronschedule.html I think that the configuration string should be this one:

sched.add_cron_job(job_function, month='1-2', day='mon-thu', hour='0-12-23', minute=30)

but I'm not sure if this is correct, someone have better knowledge on the subject and can give me a hint?

Also to choose a arbitray minutes is possible to setup the scheduler with the minutes specified inside the hour tag like:

hour = '0:15-12:10-23:30'

to fire event at 00:15, 12:10 and 23:30?

Thanks in advance and

best regards
image2.jpg (87.12 KiB) Viewed 1521 times
image1.jpg (86.8 KiB) Viewed 1521 times
Last edited by Grinch on Mon Oct 07, 2013 7:45 am, edited 2 times in total.
Posts: 10
Joined: Wed Sep 04, 2013 10:00 am

Re: APScheduler info

Postby Grinch » Mon Oct 07, 2013 7:20 am

Dear All,
problem solved, the correct syntax is reported below:

sched.add_cron_job(job_function, month='1-2', day='mon-thu', hour='0,12,23', minute=30)

actually is not possible to set a different minute starting time, if you need this feature the only way is define and add new separate scheduled job, then the minute parameter affect in a same way all the hour specified times.

Best regards
Posts: 10
Joined: Wed Sep 04, 2013 10:00 am

Re: APScheduler info example as to terminate a scheduled tas

Postby Grinch » Mon Oct 07, 2013 7:44 am

Dear All,
here below an example how is possible to exit from a running task without shutting down the task in a forcibly way. To achieve this you can use a global flag variable and use it to exit the task in a clean way.
Into the following code there are two flag:

- TerminateJob
- JobIsTerminated

the TerminateJob flag is used to signal to the task that is time to quit, hence the task perform all the needed operation to exit in a clean way respect the whole application. Meantime that the task is performing the closing operations, somewhere into the main code, we are waiting for the task ending by continuously checking the JobIsTerminated flag. Just before the task exit as a last instruction we have simply to set the JobIsTerminated flag, in this way, from the main code, we are able to detect that the task have quit all the operations and then we can proceed to remove the job from the scheduler in a clean way.

Additional notes
Inside of the reported example is not handled the possible condition of a crashing into the task code. In that sense, in order to prevent any code stucking due to the waiting loop for the JobIsTerminated flag setting, you shall provide a simple timeout mechanism in order to know if the task have problem in quitting and then perform the necessary actions.


Code: Select all
# ----------------------------------------------------------
# Powermos 03/10/2013
# Example how to kill a thread from scheduler in a clean way
# in responce to this post:
# https://groups.google.com/d/msg/apscheduler/WB_9Z0FllJk/Uatm2nse8xYJ
# https://groups.google.com/d/msg/apscheduler/D0mn505ZAYc/Sbe9mrA73E4J
# ----------------------------------------------------------

from apscheduler.scheduler import Scheduler
import time
import os
import sys
from datetime import datetime
import logging

# Global flag definition
TerminateProgram = False
TerminateJob = False
JobIsTerminated = False

# Define the function that is to be executed
# it will be executed in a thread by the scheduler
def myjob(arg1, arg2):
      global TerminateJob
      global JobIsTerminated
      JobIsTerminated = False
      print("Starting job "+arg1+" at "+str(datetime.now()))
      print("Iteration number: "+str(arg2))
      # Code start of your routine ....
      while c < arg2:
            if TerminateJob:
      # Job finished
      print("Job "+arg1+" finished with counter = "+str(c))
      JobIsTerminated = True

# ----------------------------------------------------------
# ----------------------------------------------------------
if __name__ == "__main__":

      # For the scheduler

      # Start the scheduler
      job = sched.add_cron_job(myjob, args=["myjob", 20], month = '*', day_of_week='*', hour='*', minute='*', second=5)
      while not TerminateProgram:
            print("1  Kill Current Job")
            print("2  Print Current Schedules")
            print("3  Terminate Program")
            menu=int(raw_input('Enter a number:'))
            if menu == 1:
                  print("Killing Current Job!")
                  TerminateJob = True
                  # Wait until myjob task exit
                  while not JobIsTerminated:
                  # Remove the job from the scheduler
                  # Force the scheduler termination
                  print("Scheduler shutdown Complete")
            elif menu == 2:
            elif menu == 3:
                print "Force main program exit"
                TerminateProgram = True
APScheduler: example how to quit a scheduled task in a clean way
(2.6 KiB) Downloaded 115 times
Posts: 10
Joined: Wed Sep 04, 2013 10:00 am

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests