code: def main(self):
          
  
          while True:
              while self.ready:
                  task, a = self.ready.popleft()
                  self.run_task(task, *a)
  
              if self.scheduled:
                  timeout = self.scheduled.timeout()
                  # run overdue scheduled immediately
                  if timeout < 0:
                      task, a = self.scheduled.pop()
                      self.run_task(task, *a)
                      continue
  
                  # if nothing registered, just sleep until next scheduled
                  if not self.registered:
                      time.sleep(timeout)
                      task, a = self.scheduled.pop()
                      self.run_task(task, *a)
                      continue
              else:
                  timeout = -1
  
              # TODO: add better handling for deadlock
              if not self.registered:
                  self.stopped.send(True)
                  return
  
              # run poll
              events = None
              try:
                  events = self.poll.poll(timeout=timeout)
              # IOError from a signal interrupt
              except IOError:
                  pass
              if events:
                  self.spawn(self.dispatch_events, events)
docs: Scheduler steps:
              - run ready until exhaustion
  
              - if there's something scheduled
                  - run overdue scheduled immediately
                  - or if there's nothing registered, sleep until next scheduled
                    and then go back to ready
  
              - if there's nothing registered and nothing scheduled, we've
                deadlocked, so stopped
  
              - poll on registered, with timeout of next scheduled, if something
                is scheduled