code: def pop_events(self, regex_pattern, timeout):
          if not self.started:
              raise IllegalStateError(
                  "Dispatcher needs to be started before popping.")
          deadline = time.time() + timeout
          while True:
              results = self._match_and_pop(regex_pattern)
              if len(results) != 0 or time.time() > deadline:
                  break
              time.sleep(1)
          if len(results) == 0:
              raise queue.Empty('Timeout after {}s waiting for event: {}'.format(
                  timeout, regex_pattern))
          return sorted(results, key=lambda event: event['time'])