Implement functionality of a timer in event-action paradigm.
The API of the timer should be the following:
class TimerModule:
## Actions
def start_timer(self, timer_name, duration):
""" Schedule a new timer """
def stop_timer(self, timer_name):
""" Stop a timer """
## Listeners
def timer_expired_events(self):
""" Generator that returns events from timer.
This code will be executed in another thread from actions
Yields: dict
"""
while True:
yield dict()
Note that listener is started in a separate thread. How would your solution change if it was in another process? It would be good if it doesn't require much change. You may use cron, celery or some other libraries that fit the purpose.
Nice code
- Readability
- Versatility
- Handling of unknown action arguments and desicions on which types to support