Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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_Networking_Sockets_Twisted - Fatal编程技术网

Python 是否可以在Twisted中的套接字上设置超时?

Python 是否可以在Twisted中的套接字上设置超时?,python,networking,sockets,twisted,Python,Networking,Sockets,Twisted,我意识到我可能只是愚蠢,错过了一些重要的事情,但我不知道如何使用reactor.listenUDP在twisted中指定超时。我的目标是能够指定一个超时,在所说的时间量之后,如果DatagramProtocol.datagramReceived尚未执行,则让它执行回调或我可以用来调用reactor.stop()的东西。任何帮助或建议都将不胜感激。谢谢因为Twisted是事件驱动的,所以您本身不需要超时。当您收到数据报时,只需设置一个状态变量(如datagramRecieved),并注册一个检查状

我意识到我可能只是愚蠢,错过了一些重要的事情,但我不知道如何使用reactor.listenUDP在twisted中指定超时。我的目标是能够指定一个超时,在所说的时间量之后,如果DatagramProtocol.datagramReceived尚未执行,则让它执行回调或我可以用来调用reactor.stop()的东西。任何帮助或建议都将不胜感激。谢谢

因为Twisted是事件驱动的,所以您本身不需要超时。当您收到数据报时,只需设置一个状态变量(如datagramRecieved),并注册一个检查状态变量、停止反应器(如果合适)然后清除状态变量的变量:

from twisted.internet import task
from twisted.internet import reactor

datagramRecieved = False
timeout = 1.0 # One second

# UDP code here

def testTimeout():
    global datagramRecieved
    if not datagramRecieved:
        reactor.stop()
    datagramRecieved = False


l = task.LoopingCall(testTimeout)
l.start(timeout) # call every second

# l.stop() will stop the looping calls
reactor.run()

我认为
reactor.callLater
LoopingCall
更有效。大概是这样的:

class Protocol(DatagramProtocol):
    def __init__(self, timeout):
        self.timeout = timeout

    def datagramReceived(self, datagram):
        self.timeout.cancel()
        # ...

timeout = reactor.callLater(5, timedOut)
reactor.listenUDP(Protocol(timeout))

对于反应堆,我们以后必须使用。 ConnectionMode时启动超时倒计时。 收到线路时重置超时倒计时

这是我的建议

# -*- coding: utf-8 -*-

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor, defer

_timeout = 27


class ServiceProtocol(LineReceiver):

    def __init__(self, users):
        self.users = users


    def connectionLost(self, reason):
        if self.users.has_key(self.name):
            del self.users[self.name]

    def timeOut(self):
        if self.users.has_key(self.name):
            del self.users[self.name]
        self.sendLine("\nOUT: 9 - Disconnected, reason: %s" % 'Connection Timed out')
        print "%s - Client disconnected: %s. Reason: %s" % (datetime.now(), self.client_ip, 'Connection Timed out' )
        self.transport.loseConnection()

    def connectionMade(self):
        self.timeout = reactor.callLater(_timeout, self.timeOut)

        self.sendLine("\nOUT: 7 - Welcome to CAED")

    def lineReceived(self, line):
        # a simple timeout procrastination
        self.timeout.reset(_timeout)

class ServFactory(Factory):

    def __init__(self):
        self.users = {} # maps user names to Chat instances

    def buildProtocol(self, addr):
        return ServiceProtocol(self.users)

port = 8123
reactor.listenTCP(port, ServFactory())
print "Started service at port %d\n" % port
reactor.run()

更好的方法是使用
twisted.protocols.policies.TimeoutMixin
。它本质上是做一个
callLater
,但抽象为一个
Mixin