Need to kill a running thread started by APscheduler

3,944 views
Skip to first unread message

Brian F

unread,
May 29, 2013, 11:11:06 PM5/29/13
to apsch...@googlegroups.com
To start with I just started trying to program again after not touching it for years so I'm very rusty.  I'm using APscheduler to run a function daily and eventually multiple functions, that can take hours to complete. I want to be able to terminate the running thread APscheduler started if so choosen. I tried sched.shutdown(wait=false) which stopped the exception from being thrown but the thread continues to run and it still shows up under sched.print_jobs().  The running program runs some GPIO pins currently attached to LEDs, and I can continue to see them run even though the program has exited.  Any help is greatly appreciated!


--------------------Program----------------------------------------------
from apscheduler.scheduler import Scheduler
from time import sleep
import os

sched=Scheduler()

Terminate = False

@sched.cron_schedule(hour=20, minute=31)
def myfunciton():
      print 'Does something that runs for hours'
sched.start()

os.system("clear")
while not Terminate:
        print 'Menu'
        print "2  Kill Current Process"
        print "3  Print Current Schedules"
        print "9  Terminate Program"
        menu=int(raw_input('Enter a number:'))
        if menu == 2:
                print "Canceling Running Program!!"
                sched.shutdown(wait=False)
                print "sched.shutdown Complete"
        elif menu == 3:
                sched.print_jobs()
        elif menu == 9:
                print "Halting"
-----------------------Output---------------------------------------
An example out of print job, terminate, print job, exit: (I've removed the menu's to save space)

Menu
2  Kill Current Process
3  Print Current Scheudles
9  Terminate Program

Jobstore default:
    myfunction (trigger: cron[hour='21', minute='6'], next run at: 2013-05-29 21:06:00)

myfunction has started

Enter a number:3
Jobstore default:
    myfunction (trigger: cron[hour='21', minute='6'], next run at: 2013-05-30 21:06:00)

Enter a number:2
Canceling Running Program!!
sched.shutdown Complete

Enter a number:3
Jobstore default:
    run_water (trigger: cron[hour='21', minute='6'], next run at: 2013-05-30 21:06:00)
Menu
1  Run Single Zone
2  Kill Current Process
3  Print Current Scheudles
9  Terminate Program
Enter a number:9
Halting


Alex Grönholm

unread,
May 29, 2013, 11:43:18 PM5/29/13
to apsch...@googlegroups.com
30.05.2013 06:11, Brian F kirjoitti:
> To start with I just started trying to program again after not
> touching it for years so I'm very rusty. I'm using APscheduler to run
> a function daily and eventually multiple functions, that can take
> hours to complete. I want to be able to terminate the running thread
> APscheduler started if so choosen. I tried sched.shutdown(wait=false)
> which stopped the exception from being thrown but the thread continues
> to run and it still shows up under sched.print_jobs(). The running
> program runs some GPIO pins currently attached to LEDs, and I can
> continue to see them run even though the program has exited. Any help
> is greatly appreciated!
Threads cannot be forcibly terminated, that could result in corruption
of the system state. I'm not sure what is supposed to happen to turn off
the LEDs but if terminating the whole process does not help then there's
not much to be done. This isn't something that can be fixed if threads
are used. The only solution would be to use a subprocess based executor,
but another user reported an error with that. I plan to rectify this
issue in APScheduler 3.0 if possible.
> --
> 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.
>
>

Powermos Grinch

unread,
Oct 3, 2013, 10:43:49 AM10/3/13
to apsch...@googlegroups.com
Hy Brian,
I've coded a possible solution, don't know if this a really clean solution or not but seems to work.
 
 
Bye
Powermos

Alex Grönholm

unread,
Oct 3, 2013, 11:16:43 AM10/3/13
to apsch...@googlegroups.com
03.10.2013 17:43, Powermos Grinch kirjoitti:
Hy Brian,
I've coded a possible solution, don't know if this a really clean solution or not but seems to work.
 
It's not a general solution, which is what I thought the original poster wanted. That is, if execution is stuck in third party code, this won't work.
Reply all
Reply to author
Forward
0 new messages