Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute scheduling? #145

Open
AstraLuma opened this issue Jun 21, 2017 · 1 comment
Open

Absolute scheduling? #145

AstraLuma opened this issue Jun 21, 2017 · 1 comment

Comments

@AstraLuma
Copy link

I know that to do this kind of thing "properly", you need a bunch of other stuff.

To be honest, I'm just hoping to have some stuff added so that I don't need to run both schedule and sched in the same process.

This would be really handy for in-memory objects or other resources that are tied to process lifecycle. (My specific use case is remote access credentials that expire. I get the timestamp of exactly when they expire, and I don't care if the process restarts because I just reauthenticate at start.)

@AstraLuma
Copy link
Author

AstraLuma commented Jun 21, 2017

My work-around for this is:

import threading

class ScheduleThread(threading.Thread):
    def __init__(self, *pargs, **kwargs):
        super().__init__(*pargs, daemon=True, name="scheduler", **kwargs)
        import sched
        import time
        self.sched = sched.scheduler(time.time, time.sleep)

    def run(self):
        import schedule
        import time
        while True:
            schedule.run_pending()

            nextsched = self.sched.run(False)
            if nextsched is None:
                nextsched = float('inf')

            try:
                nextschedule = schedule.idle_seconds()
            except Exception:
                nextschedule = float('inf')

            if nextsched == nextschedule == float('inf'):
                time.sleep(1)  # Finish init
            else:
                time.sleep(min(nextsched, nextschedule))

    def every(self):
        import schedule
        return schedule.every()

    def when(self, time, callable, *pargs, **kwargs):
        self.sched.enterabs(time, 0, callable, pargs, kwargs)

    def delay(self, delay, callable, *pargs, **kwargs):
        self.sched.enter(delay, 0, callable, pargs, kwargs)

schedule = ScheduleThread()
schedule.start()

Which I'm not in love with, but it seems to be working.

EDIT: The latest version of this is https://github.com/astronouth7303/xontrib-schedule/blob/master/xontrib/schedule.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant