Python Forum
Stopping scheduler with condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stopping scheduler with condition
#12
The following works for your situation, but breaks the multi tasking (since I re-initialize the thread list).
It needs more work, but can be used temporarily.
I also fixed PEP 8 Issues
#
# Author: Larry McCaig (Larz60+)
# No warranty, use at your own risk!
# Modify if you wish
#
import threading


class EventTimer:
    """
    Creates overlaping threaded timed events
    Events can be set to trigger in days, hours, minutes, and/or seconds
    Any number of events can be created, and are treated independently, thus can
    overlap each other.
    Program execution will continue until all events have completed.
    the function that is passed to each timer will execute after the timeout has
    been reached
    """

    def __init__(self):
        self.threadList = []

    def add_event(self, func, days=0, hours=0, minutes=0, seconds=0, new=False):
        if new:
            self.threadList = []
        times = [days, hours, minutes, seconds]
        multiplier = [86400, 3600, 60, 1]
        count = 0

        for n in range(len(times)):
            count += times[n] * multiplier[n]

        t = threading.Timer(count, func)
        self.threadList.append(t)

    def start_all(self):
        # Start threads
        [th.start() for th in self.threadList]

    def wait_for_finish(self):
        # wait for all to finish
        [th.join() for th in self.threadList]


class MyTimedEvent(EventTimer):
    def __init__(self, max_iters=1, seconds=0):
        super().__init__()
        self.num_iterations_so_far = 0
        self.max_iters = max_iters
        self.seconds = seconds

    def kick_off_event(self):
        self.add_event(self.event_triggered, seconds=self.seconds, new=True)
        self.start_all()

    def event_triggered(self):
        self.max_iters -= 1
        print('T minus {}'.format(self.max_iters))
        if self.max_iters:
            self.kick_off_event()


def launch():
    mte = MyTimedEvent(max_iters=8, seconds=2)
    mte.kick_off_event()
    mte.wait_for_finish()


if __name__ == '__main__':
    launch()
The following is my initial attempt at a new timer class that allows for recurring tasks - I haven't implemented the code that
will start and restart the timer threads yet - still thinking about this one and open for all suggestions
#
# Author: Larry McCaig (Larz60+)
# No warranty, use at your own risk!
# Modify if you wish
#
import threading


class EventTimer:
    """
    Creates overlapping threaded timed events
    Events can be set to trigger in days, hours, minutes, and/or seconds
    Any number of events can be created, and are treated independently, thus can
    overlap each other.
    Program execution will continue until all events have completed.
    the function that is passed to each timer will execute after the timeout has
    been reached
    """

    def __init__(self):
        self.threadList = []

    def add_event(self, func, days=0, hours=0, minutes=0, seconds=0, recurring=False, max_iters=1):
        times = [days, hours, minutes, seconds]
        multiplier = [86400, 3600, 60, 1]
        count = 0

        for n in range(len(times)):
            count += times[n] * multiplier[n]

        t = threading.Timer(count, func)
        self.threadList.append((t, recurring, max_iters))

    def start_all(self):
        # Start threads
        [th.start() for th in self.threadList]

    def wait_for_finish(self):
        # wait for all to finish
        [th.join() for th in self.threadList]

    def show_threadlist(self) -> object:
        n = 0
        for t, recurring, max_iters in self.threadList:
            print('Index: {}, thread: {}, recurring: {} max_iters: {}'.format(
                n, t, recurring, max_iters))
            n += 1
        return None


def function1():
    print('Function 1 triggered')


def function2():
    print('Function 2 triggered')


def function3():
    print('Function 3 triggered')

def launch():
    e = EventTimer()
    e.add_event(function1, seconds=5)
    e.add_event(function2, seconds=10)
    e.add_event(function3, seconds=2, recurring=True, max_iters=5)
    e.show_threadlist()


if __name__ == '__main__':
    launch()
Reply


Messages In This Thread
Stopping scheduler with condition - by j.crater - Dec-22-2016, 08:08 AM
RE: Stopping scheduler with condition - by wavic - Dec-22-2016, 09:31 AM
RE: Stopping scheduler with condition - by j.crater - Dec-22-2016, 09:40 AM
RE: Stopping scheduler with condition - by wavic - Dec-22-2016, 09:46 AM
RE: Stopping scheduler with condition - by Larz60+ - Dec-22-2016, 12:30 PM
RE: Stopping scheduler with condition - by j.crater - Jan-03-2017, 09:19 AM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 10:53 AM
RE: Stopping scheduler with condition - by j.crater - Jan-03-2017, 01:06 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 01:09 PM
RE: Stopping scheduler with condition - by j.crater - Jan-03-2017, 01:12 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 01:15 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 05:32 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-03-2017, 10:50 PM
RE: Stopping scheduler with condition - by j.crater - Jan-04-2017, 06:36 AM
RE: Stopping scheduler with condition - by j.crater - Jan-09-2017, 03:02 PM
RE: Stopping scheduler with condition - by Larz60+ - Jan-09-2017, 07:28 PM
RE: Stopping scheduler with condition - by j.crater - Jan-10-2017, 06:31 AM
RE: Stopping scheduler with condition - by Larz60+ - Jan-10-2017, 11:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python script through Task Scheduler? Winfried 8 661 Mar-10-2024, 07:24 PM
Last Post: Winfried
  stopping number conversion before end Skaperen 6 3,072 Jul-12-2020, 09:22 AM
Last Post: DeaD_EyE
  Stopping a loop in another file catosp 4 2,750 Jun-15-2020, 02:45 PM
Last Post: catosp
  else condition not called when if condition is false Sandz1286 10 5,976 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,279 Jun-01-2020, 06:00 PM
Last Post: penahuse
  how to cancel scheduler module event nanok66 0 2,170 May-11-2020, 10:31 PM
Last Post: nanok66
  Getting an .exe to run from Task Scheduler fioranosnake 2 3,059 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Simple mutli-threaded scheduler using sched - stuck Mzarour 2 6,188 Nov-12-2019, 07:44 PM
Last Post: Mzarour
  Batch file not running python script in task scheduler davork 3 4,577 May-09-2019, 12:53 PM
Last Post: Gribouillis
  Help with Stopping a function after a timer SheeppOSU 0 1,963 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020