title: Alternative to a while loop in twisted which doesn't block the reactor thread
body: <p>I'm making a chat application in twisted. Suppose my server is designed in such a way that whenever it detects a client online, it sends the client all the pending-messages (those messages of that client which were cached in a python-list on the server because it was offline) one-by-one in a while loop until the list is exhausted. Something like this:</p>
  
  <pre><code>class MyChat(LineReceiver):
  
      def connectionMade(self):
          self.factory.clients.append(self)
  
          while True: 
              #retrieve first message from a list of pending-messages(queue) of "self" 
              msg = self.retrieveFromQueue(self)      
              if msg != "empty":
                  self.transport.write(msg)       
              else:
                  break
  
      def lineReceived(self, line):
      ...
  
      def connectionLost(self, reason):
      ...  
  
      def retrieveFromQueue(self, who):
          msglist = []                                                        
  
          if who in self.factory.userMessages:
              msglist = self.factory.userMessages[who]
  
          if msglist != []:
              msg = msglist.pop(0)              #msglist is a list of strings
  
              self.factory.userMessages[self] = msglist               
  
              return msg
          else:
              return "empty"  
  
  
      factory.userMessages = {}   #dict of list of incoming messages of users who aren't online
  </code></pre>
  
  <p>So according to my understanding of Twisted, the while loop will block the main reactor thread and any interaction from any other client with the server will not be registered by the server. If that's the case, I want an alternate code/method to this approach which will not block the twisted thread.</p>
  
  <p><strong>Update:</strong> There may be 2000-3000 pending messages per user because of the nature of the app.</p>