old_contents: """
  
  Very basic implementation of an event loop
  ==========================================
  
  The eventloop is a singleton to schedule and run events.
  
  Exports
  -------
  
  - ``EventLoop``: the basic eventloop
  
  """
  
  import queue
  import threading
  import time
  
  from .singleton import Singleton
  
  
  class EventLoop(object, metaclass=Singleton):
      """A generic event loop object."""
  
      def __init__(self):
          self.queue = queue.Queue()
  
      def schedule(self, event):
          """Schedule an event.
  
          An `event` is a thunk.
  
          """
          self.queue.put(event)
  
      def stop(self):
          """Stop the loop."""
          pass
          
      def run_step(self, block=True):
          """Process one event."""
          ev = self.queue.get(block=block)
          ev()
  
      def run(self, block=False):
          """Process all events in the queue."""
          try:
              while True:
                  self.run_step(block=block)
          except queue.Empty:
              return
              
      def run_in_thread(self):
          self.thread = threading.Thread(target=self.run, args=(True,),
                                         name='event_loop')
          self.thread.daemon = True
          self.thread.start()
          
  
new_contents: """
  
  Very basic implementation of an event loop
  ==========================================
  
  The eventloop is a singleton to schedule and run events.
  
  Exports
  -------
  
  - ``EventLoop``: the basic eventloop
  
  """
  
  import queue
  import sched
  import threading
  import time
  
  from .singleton import Singleton
  
  
  class EventLoop(object, metaclass=Singleton):
      """A generic event loop object."""
  
      def __init__(self):
          self.scheduler = sched.scheduler()
  
      def schedule(self, event):
          """Schedule an event.
  
          An `event` is a thunk.
  
          """
          self.scheduler.enter(0, 1, event)
  
      def stop(self):
          """Stop the loop."""
          pass
          
      def run(self, block=False):
          self.scheduler.run(blocking=block)
  
      def run_in_thread(self):
          self.thread = threading.Thread(target=self.run, args=(True,),
                                         name='event_loop')
          self.thread.daemon = True
          self.thread.start()
          
  
subject: Use Python scheduler rather than the home-made one