Stop a task which is currently running

2,164 views
Skip to first unread message

vivek vardhan

unread,
Jun 20, 2012, 12:23:02 PM6/20/12
to apsch...@googlegroups.com
Hello, 

Is there any possible way to stop a task which is already running ?? 

for e.g. if Task A scheduled to be triggered after 10 mins, has been triggered and is currently running. But for some reason i would like to stop this Task A from running. is it possible to do that.. ?? its not the un-scheduling of an already scheduled job, but to stop a currently running scheduled job

Cheers, 
Vivek

Alex Grönholm

unread,
Jun 20, 2012, 12:26:45 PM6/20/12
to apsch...@googlegroups.com
20.06.2012 19:23, vivek vardhan kirjoitti:
> Hello,
>
> Is there any possible way to stop a task which is already running ??
No.

Powermos Grinch

unread,
Oct 3, 2013, 8:28:40 AM10/3/13
to apsch...@googlegroups.com

Il giorno mercoledì 20 giugno 2012 18:26:45 UTC+2, Alex Grönholm ha scritto:
20.06.2012 19:23, vivek vardhan kirjoitti:
> Hello,
>
> Is there any possible way to stop a task which is already running ??
No.
May be a idea set up a global flag used to exit the task activity?
When I've to stop (then exit) from the task I can simply set this global flag e.g. from the main code, obviously into the task I've to perform a check for the flag state and if this is trigged I can simply perform a clean exit from the task routine, at this point the scheduler should be able to detect the exit.
I'm right or this is not a clean way to do it?
 
Thanks
Powermos

Powermos Grinch

unread,
Oct 3, 2013, 10:28:09 AM10/3/13
to apsch...@googlegroups.com, vivek.v...@gmail.com
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

Alex Grönholm

unread,
Oct 3, 2013, 11:12:25 AM10/3/13
to apsch...@googlegroups.com
If your code cooperates, then sure, a flag would be a good way to exit. But there's no way to forcibly end the task.
 
Thanks
Powermos
--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages