简体   繁体   中英

Twisted dataReceived hangs

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?

My code is as follows:

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) > 1 and (len(self.factory.pushstack) < 100):
                    self.payload += data[1:]
                    self.bytesremaining -= len(data) - 1
            self.headerseen = True
    elif len(self.factory.pushstack) < 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)

It appears to me that the default twisted reactor for OS X (selectreactor) is not as stable as kqueue.

I am not seeing the issue anymore after installing the kqueue reactor.

from twisted.internet import kqreactor
kqreactor.install()
from twisted.internet import reactor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM