Skip to content

Instantly share code, notes, and snippets.

@danlkv
Last active June 3, 2020 00:58
Show Gist options
  • Save danlkv/6dd05bbdb1e8852f20480baff1716926 to your computer and use it in GitHub Desktop.
Save danlkv/6dd05bbdb1e8852f20480baff1716926 to your computer and use it in GitHub Desktop.

Timer module

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.

Criteria

Nice code

  • Readability
  • Versatility
  • Handling of unknown action arguments and desicions on which types to support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment