dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.73k stars 959 forks source link

Compiling with Scheduler, How to stop all threads/schedules to terminate .exe? #509

Open gitagogaming opened 2 years ago

gitagogaming commented 2 years ago

Hello and goodday to you I have created a .exe using scheduler and after compiling, when the program is told to shut down it still hangs/remains in the background.

I'm thinking there is a bug as when I check jobs, they all show as cleared, but also likely its user error on my part.. So onto explaining...

I've removed scheduling out of the equation and then it shuts down as expected. These are the commands im trying to shut down everything with.

     running = False
    stop_run_continuously.set()
    stop_run_continuously.clear()
    schedule.clear()
    TPClient.disconnect()
    Timer.cancel()

This is how I am starting the schedules

    for scheduleFunc in [(disk_usage, "Update Interval: Hard Drive"),
                         (get_ip_loop, "Update Interval: Public IP"),
                         (check_number_of_monitors, "Update Interval: Active Monitors"),
                         (get_windows_update, "Update Interval: Active Windows"), 
                         (vd_check, "Update Interval: Active Virtual Desktops")]:
        interval = float(newsettings[scheduleFunc[1]])
        if int(newsettings[scheduleFunc[1]]) > 0:
            schedule.every(interval).seconds.do(scheduleFunc[0])
            print(f"{scheduleFunc[1]} is now {interval}")
        else:
            print(f"{scheduleFunc[1]} is TURNED OFF")

I am also utilizing the code below which I acquired from here

import threading
import time

import schedule

def run_continuously(interval=1):
    """Continuously run, while executing pending jobs at each
    elapsed time interval.
    @return cease_continuous_run: threading. Event which can
    be set to cease continuous run. Please note that it is
    *intended behavior that run_continuously() does not run
    missed jobs*. For example, if you've registered a job that
    should run every minute and you set a continuous run
    interval of one hour then your job won't be run 60 times
    at each interval but only once.
    """
    cease_continuous_run = threading.Event()

    class ScheduleThread(threading.Thread):
        @classmethod
        def run(cls):
            while not cease_continuous_run.is_set():
                schedule.run_pending()
                time.sleep(interval)

    continuous_thread = ScheduleThread()
    continuous_thread.start()
    return cease_continuous_run