title: Twisted dataReceived hangs
body: <p>I am using python twisted library and am making a server to receive data to do some processing on the received data and then close the connection. I observe that the program hangs in dataReceived without a print statement. With print statement it goes through. Wondering is print is somehow slowing down the execution to avoid race condition or if I have coded a bug?</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>class Stack(Protocol):
     def __init__(self, factory):
      self.factory = factory
      self.bytesremaining = None
      self.payload = ""
      self.headerseen = False
     def dataReceived(self, data):
      if self.headerseen == False:
              header = unpack('B',data[0])[0]
              if header == 128:
                      self.pop()
                      return
              self.bytesremaining = self.datalength = unpack('B',data[0])[0]
              print self.datalength #without this print the execution hangs in the middle.
              if len(data) &gt; 1 and (len(self.factory.pushstack) &lt; 100):
                      self.payload += data[1:]
                      self.bytesremaining -= len(data) - 1
              self.headerseen = True
      elif len(self.factory.pushstack) &lt; 100:
              self.payload += data
              self.bytesremaining -= len(data) - 1
      if self.bytesremaining == 0:
              self.factory.pushstack.appendleft(self.payload)
              retval = pack('B',0)
              self.transport.write(retval)
              self.transport.loseConnection()
  
  class StackFactory(ServerFactory):
    def __init__(self):
      self.clients = []
      self.pushstack = collections.deque()
      self.popstack = collections.deque()
      self.clientsmap = {}
  
    def buildProtocol(self, addr):
      return Stack(self)
  </code></pre>