python - 链接延迟回调

标签 python twisted deferred asynchronous-messaging-protocol

我正在尝试在 AMP 客户端中链接延迟,如下所示:

客户:

from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
from twisted.protocols.amp import AMP

import commands

def connect_protocol(host, port):
    destination = TCP4ClientEndpoint(reactor, host, port)
    d = connectProtocol(destination, AMP())

    def connect(protocol):
        print 'Connecting to server as Mr Spaceman...'
        return protocol.callRemote(commands.Connect,
                                   username='Mr Foo')

    def say(protocol):
        print 'Saying "Hello world" to the server...'
        return protocol.callRemote(commands.Say,
                                   phrase='Hello world')

    d.addCallback(connect)
    d.addCallback(say)


def main(host, port):
    connect_protocol(host, port)
    print 'Connected to %s:%d...' % (host, port)
    reactor.run()

main('127.0.0.1', 12345)

服务器:

from twisted.internet.protocol import Factory
from twisted.protocols.amp import AMP

import commands

class CommandProtocol(AMP):

    def connect(self, username):
        print "Received connect command: %s." % (username)
        return {}
    commands.Connect.responder(connect)

    def say(self, phrase):
        print "Received phrase \"%s\"." % phrase
        return {}
    commands.Say.responder(say)

def main(port):
    factory = Factory()
    factory.protocol = CommandProtocol
    reactor.listenTCP(port, factory)
    print 'Started AMP server on port %d...' % port
    reactor.run()

main(12345)

只有 connect() 在服务器端被触发

最佳答案

首先,启用日志记录:

from sys import stdout
from twisted.python.log import startLogging
startLogging(stdout)

现在您将看到程序中发生了什么。

其次,至少有一个最终的 errback 来记录 Deferred 上未处理的故障,这样这些故障就会确定地显示出来,而不是依赖于垃圾收集器:

from twisted.python.log import err

...

    d.addCallback(connect)
    d.addCallback(say)
    d.addErrback(err, "connect_protocol encountered some problem")

最后,Deferred 的结果会被附加的回调和 errback 改变。在这种情况下,传递给 say 的参数是 connect 返回的 Deferred 的结果。这与 connect 的参数不同,因此您不太可能在其上使用 callRemote

您可以通过多种不同的方式解决此问题。涉及最少代码更改的一种方法(但不一定是最佳解决方案)是将协议(protocol)作为 connect Deferred 结果中的额外值传递:

def connect(protocol):
    print 'Connecting to server as Mr Spaceman...'
    d = protocol.callRemote(commands.Connect, username='Mr Foo')
    d.addCallback(lambda result: (protocol, result))
    return d

def say((protocol, result)):
    print 'Saying "Hello world" to the server...'
    return protocol.callRemote(commands.Say,
                               phrase='Hello world')

关于python - 链接延迟回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244681/

相关文章:

python - 欧拉计划 #255

AngularJS : $q -> deferred API order of things (lifecycle) AND who invokes digest?

jquery - .resolve() 和 .promise() 有什么区别?

python - 如果坐标不超过阈值,则对列进行分组

python - 如何删除使用 Python 的 easy_install 安装的软件包?

python - 限制 Twisted FTP 服务器中的文件大小

python - 有没有什么办法可以用扭曲的方式异步发布一些数据?

javascript - 仅在不可执行的异步函数完成后才执行代码

Python:Factory Boy 生成对象创建时指定的长度列表

python - 在 ssl 服务器/客户端示例中乘以验证回调/getContext 调用