Hi,
I've did a simple code to proof my theory, I'm a noob then may be this is not a clean way but I wait for some expert to have criticism or suggestion in order to improve it:
# ----------------------------------------------------------
# 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# ----------------------------------------------------------
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():
global TerminateJob
global JobIsTerminated
JobIsTerminated = False
print("Starting job at "+str(datetime.now()))
# Code start of your routine ....
c=0
while c < 20:
print("work")
time.sleep(1)
c=c+1
if TerminateJob:
break
# Job finished
print("Job finished with counter = "+str(c))
JobIsTerminated = True
# ----------------------------------------------------------
# MAIN
# ----------------------------------------------------------
if __name__ == "__main__":
# For the scheduler
logging.basicConfig()
# Start the scheduler
sched=Scheduler()
job = sched.add_cron_job(myjob, month = '*', day_of_week='*', hour='*', minute='*', second=5)
sched.start()
os.system("clear")
while not TerminateProgram:
print("Menu")
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:
pass
# Remove the job from the scheduler
sched.unschedule_job(job)
# Force the scheduler termination
sched.shutdown(wait=False)
print("Scheduler shutdown Complete")
elif menu == 2:
sched.print_jobs()
elif menu == 3:
print "Force main program exit"
TerminateProgram = True
Regards
Powermos