Python Forum
Multi threaded Timer Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi threaded Timer Class
#1
Moved from python-forum.org

Here is a timer class that can set to run simultaneous timers and kick off events upon completion.
a timer can be set to wait days, hours, minutes, and/or seconds. all overlapping.
Other processes can be run while waiting for events, or you can wait until all are done to continue (your choice).

Here's the code with built in test.
#
# Author: 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 addEvent(self, func, days=0, hours=0, minutes=0, seconds=0):
        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 startAll(self):
        # Start threads
        [th.start() for th in self.threadList]

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

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

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

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

def finalone():
    print('Final function triggered')

def launch():
    e = EventTimer()
    e.addEvent(function1, seconds=5)
    e.addEvent(function2, seconds=10)
    e.addEvent(function3, seconds=20)
    e.addEvent(finalone, seconds=25)
    e.startAll()
    print("These startement shows that you can do other things while")
    print('Waiting for events to trigger')
    print('And then wait for all to finish')
    print('Events will still trigger even if other processes are running')
    print('Only call waitForFinish when you are ready to do so')
    e.waitForFinish()

if __name__ == '__main__':
    launch()
To use,
  • Create an instance of the EventTimer class
  • example:
        timers = EventTimer()
  • Call the instance method addEvent for each timer, passing the function you want to execute after the time has elapsed, and your time (all are defaulted, so only need to pass what you need)
  • example
        timers.addEvent(MyFunc, hours=2, seconds=3)
  • start the timers with a call to instance.StartAll()
  • Do your other processing
  • wait for remaining events to finish with a call to instance.waitForFinish()
Enjoy!

Larz60+
Reply


Forum Jump:

User Panel Messages

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