Twisted延期定期电话

时间:2015-01-20 21:18:27

标签: python twisted deferred

我的问题看起来像这样: 我有一个Twisted Server,它使用callLater通过TCP每2秒发送一次值。我有一个扭曲的客户端,它接收值并应该延迟处理接收值。

我的服务器看起来像这样:

from twisted.internet.protocol import ServerFactory
from twisted.protocols.basic import NetstringReceiver
from random import randint

class ServerProtocol(NetstringReceiver):
    def __init__(self, factory):
        self.factory = factory
        self.sendObject = None

    def connectionMade(self):        
        self.sendValue()

    def connectionLost(self, reason):
        sendObject, self.sendObject = self.sendObject, None
        sendObject.cancel()

    def sendValue(self):
        data = randint(2,20)
        self.sendString(str(data))
        print('send: {0}'.format(data))

        from twisted.internet import reactor
        self.sendObject = reactor.callLater(2, self.sendValue)

class MyServerFactory(ServerFactory):

    def __init__(self):
        self.protsa = []

    def buildProtocol(self, addr):
        return ServerProtocol(self)

    def setCallback(self, callback):
        self.callback = callback    

def serverMain():

    factory = MyServerFactory()
#    factory.setCallback(generateVal)

    from twisted.internet import reactor

    port = reactor.listenTCP(2345, factory, interface='127.0.0.1')     
    print 'Serving on %s.' % (port.getHost())

    reactor.run()


if __name__ == '__main__':
    serverMain()

我的客户端看起来像这样:

from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import NetstringReceiver
from twisted.internet import defer


class ClientProtocol(NetstringReceiver):

    def stringReceived(self, string):
        print("recieved")
        self.factory.printValue(string)

    def connectionMade(self):
        print("Made Connection")

    def connetionLost(self):
        print("Connection Lost")

class MyClientFactory(ClientFactory):

    protocol = ClientProtocol

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

    def clientConnectionFailed(self, connector, reason):
        if self.deferred is not None:
            d, self.deferred = self.deferred, None
            d.errback(reason)

    def printValue(self, value):
        if self.deferred is not None:
            d, self.deferred = self.deferred, None
            d.callback(value)


def OutputValue(host, port):
    d = defer.Deferred()
    from twisted.internet import reactor
    factory = MyClientFactory(d)
    reactor.connectTCP(host, port, factory)
    return d

def clientMain():

    def writeError(err):
        print("Deferred Error!\n")
        print("Error: {0}".format(err.__str__))

    def writeValue(value):
        print("Value revieved: {0}".format(value))

    from twisted.internet import reactor

    d = OutputValue('127.0.0.1', 2345)
    d.addCallbacks(writeValue, writeError)

    reactor.run()

if __name__ == '__main__':
    clientMain()

输出:

Made Connection
recieved
Value revieved: 11
recieved
recieved
recieved

第一次一切正常。连接后,服务器发送客户端收到的值。客户喜欢延期,希望延期。第二个值不由延迟处理。我也期望因为延期只被解雇一次。 现在我想有可能通过延迟来处理每个收到的值,所以我也可以进行错误处理。

我看了一眼:

Twisted: deferred that fires repeatedly

Re-using deferred objects in Twisted

但是我无法解决我的问题。这不太常见。 或者我每次都必须断开连接?

1 个答案:

答案 0 :(得分:0)

Deferred是一个对象,表示您正在进行的特定请求的结果。但由于请求只能有一个关联的响应,就像函数只返回一次一样,Deferred只能被触发一次。

您已经有一个函数 - printValue - 在收到值并且应该打印时运行。为什么不立即打印该函数中的值,而不是寻找调用者设置的延迟?