Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python twisted中实现超时_Python_Client Server_Twisted - Fatal编程技术网

在python twisted中实现超时

在python twisted中实现超时,python,client-server,twisted,Python,Client Server,Twisted,据我所知,twisted是异步的、事件驱动的,有人告诉我他们不需要超时。我必须建立一个服务器应用程序,它将连接到100多个客户端,这些客户端是每2分钟向服务器发送数据的嵌入式机器,每个数据包或数据大小将为238-1500字节。因此,在现实生活中,tcp会将数据分为多个数据包,所以它们需要实现超时或扭曲来处理这种情况。我是新来的,有什么建议吗。我的服务器有以下代码,没有超时。在超时结束时,如果在连接保持活动状态时未收到完整的数据包,我只想丢弃数据包 class Server(LineReceive

据我所知,twisted是异步的、事件驱动的,有人告诉我他们不需要超时。我必须建立一个服务器应用程序,它将连接到100多个客户端,这些客户端是每2分钟向服务器发送数据的嵌入式机器,每个数据包或数据大小将为238-1500字节。因此,在现实生活中,tcp会将数据分为多个数据包,所以它们需要实现超时或扭曲来处理这种情况。我是新来的,有什么建议吗。我的服务器有以下代码,没有超时。在超时结束时,如果在连接保持活动状态时未收到完整的数据包,我只想丢弃数据包

class Server(LineReceiver):

  def connectionMade(self):
     self.factory.clients.append(self)
     self.setRawMode()
     self._peer = self.transport.getPeer()
     print 'Connected Client', self._peer

  def connectionLost(self, reason):
     self.factory.clients.remove(self)
     print 'Lost connection from', self._peer

  def rawDataReceived(self, data):
     inputArray = [ord(inp) for inp in data]
     #do something



def main():
   """This runs the protocol on port 8000"""
   factory = protocol.ServerFactory()
   factory.protocol = Server
   factory.clients = []
   reactor.listenTCP(8000,factory)
   reactor.run()

正如@Ashish Nitin Patil所建议的,只需切断连接即可实现超时:

from twisted.internet import reactor

# ...
def connectionMade(self):
    # ... your code
    # cancel connection in 2 minutes
    reactor.callLater(120, self.transport.loseConnection)

在超时结束时,如果在连接保持活动状态时未收到完整的数据包,我只想丢弃数据包

class Server(LineReceiver):

  def connectionMade(self):
     self.factory.clients.append(self)
     self.setRawMode()
     self._peer = self.transport.getPeer()
     print 'Connected Client', self._peer

  def connectionLost(self, reason):
     self.factory.clients.remove(self)
     print 'Lost connection from', self._peer

  def rawDataReceived(self, data):
     inputArray = [ord(inp) for inp in data]
     #do something



def main():
   """This runs the protocol on port 8000"""
   factory = protocol.ServerFactory()
   factory.protocol = Server
   factory.clients = []
   reactor.listenTCP(8000,factory)
   reactor.run()
如果您不想在超时时取消连接,则:

from time import time as timer

def connectionMade(self):
    # ... your code
    self.endtime = timer() + 120 # timeout in 2 minutes

def dataReceived(self, data):
    if timer() > self.endtime: # timeout
       if not self.have_we_received_full_packet()
          return # do nothing (discard data, the connection remains alive)
       else: 
          # timeout happened but we have a full packet, now what?
    inputArray = bytearray(data)
    #do something

创建一个函数,该函数执行连接切割。在主功能中,记录时间,当时间到时,触发该功能?是否尝试发送大小为
238-1500字节的数据?结果是什么?我在c#中创建了一个类似的应用程序,并在没有超时的情况下进行了测试。我能够接收完整的数据包,但只对2-3个客户端进行了测试,但如果他们的100-1000多个客户端每2分钟发送数据,我想他们需要超时。也许我错了?请更正。为什么使用
线路接收器
而不是
协议
?开头是否有面向行的数据,例如在http协议中(头是面向行的,体可以是任何内容),此reactor.callLater()跨连接工作。如果我们需要每个连接,我们应该怎么做?@Arockia你认为self.transport.loseConnection()有什么作用?我知道它会终止当前连接。